Skip to content

Commit

Permalink
Init cmd (#11)
Browse files Browse the repository at this point in the history
* Add init command

Squashes the 3 commands all users need to run into one.

* Avoid prompting for cluster name in create region

* Update init text

* docs: comments
  • Loading branch information
ipmb authored Feb 2, 2021
1 parent c9bebde commit 28ef330
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 62 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/functional_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ jobs:
name: Build
run: go build
-
name: Account Setup
run: ./apppack account-setup --region us-east-1 --dockerhub-username $DOCKERHUB_USERNAME --dockerhub-access-token $DOCKERHUB_ACCESS_TOKEN | tee account_setup_output.txt
timeout-minutes: 6
name: AppPack Init
run: |
./apppack init --region us-east-1 \
--dockerhub-username $DOCKERHUB_USERNAME \
--dockerhub-access-token $DOCKERHUB_ACCESS_TOKEN \
--domain testclusters.apppack.io \
--instance-class t3.micro | tee account_setup_output.txt
timeout-minutes: 9
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_ACCESS_TOKEN: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}
Expand All @@ -41,13 +46,6 @@ jobs:
AWS_ACCESS_KEY_ID: ${{ secrets.APPPACK_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.APPPACK_AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: us-east-1
-
name: Create cluster
run: ./apppack create cluster --region us-east-1 --domain testclusters.apppack.io --instance-class t3.micro
timeout-minutes: 8
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
-
name: Create app
run: |
Expand Down
41 changes: 0 additions & 41 deletions cmd/accountSetup.go

This file was deleted.

51 changes: 45 additions & 6 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/apppackio/apppack/auth"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/codebuild"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/getsentry/sentry-go"
Expand Down Expand Up @@ -186,6 +188,46 @@ func awsSession() (*session.Session, error) {

}

// hasApppackOIDC checks for existence of our OIDC Provider
// usually we check for the existence of a Cfn Stack, but these resources are global
// and Stacks are per-region, so we need to check for this resource directly
func hasApppackOIDC(sess *session.Session) (*bool, error) {
iamSvc := iam.New(sess)
resp, err := iamSvc.ListOpenIDConnectProviders(&iam.ListOpenIDConnectProvidersInput{})
if err != nil {
return nil, err
}
for _, r := range resp.OpenIDConnectProviderList {
oidcProvider, err := iamSvc.GetOpenIDConnectProvider(&iam.GetOpenIDConnectProviderInput{
OpenIDConnectProviderArn: r.Arn,
})
if err != nil {
return nil, err
}
if *oidcProvider.Url == "auth.apppack.io/" {
return aws.Bool(true), nil
}
}
return aws.Bool(false), nil
}

// stackExists checks if a named Cfn Stack already exists in the region
func stackExists(sess *session.Session, stackName string) (*bool, error) {
cfnSvc := cloudformation.New(sess)
_, err := cfnSvc.DescribeStacks(&cloudformation.DescribeStacksInput{
StackName: &stackName,
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
if fmt.Sprint(aerr.Code()) == "ValidationError" {
return aws.Bool(false), nil
}
}
return nil, err
}
return aws.Bool(true), nil
}

type stackItem struct {
PrimaryID string `json:"primary_id"`
SecondaryID string `json:"secondary_id"`
Expand Down Expand Up @@ -601,12 +643,9 @@ var accountCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
sess, err := awsSession()
checkErr(err)
ssmSvc := ssm.New(sess)
_, err = ssmSvc.GetParameter(&ssm.GetParameterInput{
Name: aws.String("/apppack/account"),
})

if err == nil {
alreadyInstalled, err := hasApppackOIDC(sess)
checkErr(err)
if *alreadyInstalled {
checkErr(fmt.Errorf("account already exists"))
}
if createChangeSet {
Expand Down
3 changes: 3 additions & 0 deletions cmd/createCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ var createClusterCmd = &cobra.Command{

func init() {
createCmd.AddCommand(createClusterCmd)
// All flags need to be added to `initCmd` as well so it can call this cmd
createClusterCmd.Flags().StringP("domain", "d", "", "parent domain for apps in the cluster")
initCmd.Flags().StringP("domain", "d", "", "parent domain for apps in the cluster")
createClusterCmd.Flags().StringP("instance-class", "i", "t3.medium", "autoscaling instance class -- see https://aws.amazon.com/ec2/pricing/on-demand/")
initCmd.Flags().StringP("instance-class", "i", "t3.medium", "autoscaling instance class -- see https://aws.amazon.com/ec2/pricing/on-demand/")
}
16 changes: 12 additions & 4 deletions cmd/createRegion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd
import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ssm"
Expand All @@ -31,10 +32,14 @@ var createRegionCmd = &cobra.Command{
Long: "*Requires AWS credentials.*",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
answers, err := askForMissingArgs(cmd, nil)
checkErr(err)
sess, err := awsSession()
checkErr(err)
questions := []*survey.Question{}
answers := make(map[string]interface{})
addQuestionFromFlag(cmd.Flags().Lookup("dockerhub-username"), &questions, nil)
addQuestionFromFlag(cmd.Flags().Lookup("dockerhub-access-token"), &questions, nil)
err = survey.Ask(questions, &answers)
checkErr(err)
ssmSvc := ssm.New(sess)
if createChangeSet {
fmt.Println("Creating Cloudformation Change Set for region-level resources...")
Expand All @@ -49,7 +54,7 @@ var createRegionCmd = &cobra.Command{
}
_, err = ssmSvc.PutParameter(&ssm.PutParameterInput{
Name: aws.String("/apppack/account/dockerhub-access-token"),
Value: getArgValue(cmd, answers, "dockerhub-access-token", true),
Value: getArgValue(cmd, &answers, "dockerhub-access-token", true),
Type: aws.String("SecureString"),
Tags: tags,
})
Expand All @@ -65,7 +70,7 @@ var createRegionCmd = &cobra.Command{
Parameters: []*cloudformation.Parameter{
{
ParameterKey: aws.String("DockerhubUsername"),
ParameterValue: getArgValue(cmd, answers, "dockerhub-username", true),
ParameterValue: getArgValue(cmd, &answers, "dockerhub-username", true),
},
},
Capabilities: []*string{aws.String("CAPABILITY_IAM")},
Expand All @@ -78,7 +83,10 @@ var createRegionCmd = &cobra.Command{

func init() {
createCmd.AddCommand(createRegionCmd)
// All flags need to be added to `initCmd` as well so it can call this cmd
createRegionCmd.Flags().StringP("dockerhub-username", "u", "", "Docker Hub username")
initCmd.Flags().StringP("dockerhub-username", "u", "", "Docker Hub username")
createRegionCmd.Flags().StringP("dockerhub-access-token", "t", "", "Docker Hub Access Token (https://hub.docker.com/settings/security)")
initCmd.Flags().StringP("dockerhub-access-token", "t", "", "Docker Hub Access Token (https://hub.docker.com/settings/security)")

}
78 changes: 78 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"

"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "setup your AppPack account and create initial resources",
Long: "*Requires AWS credentials.*\n\nThis is a shortcut for `apppack create account && apppack create region && apppack create cluster`",
Run: func(cmd *cobra.Command, args []string) {
sess, err := awsSession()
checkErr(err)
fmt.Print(aurora.Faint("==="), aurora.Bold(aurora.Blue("Welcome to AppPack!")), " 🎉\n\n")
fmt.Println("This will step you through the intial AppPack setup process.")
fmt.Println("Before getting started, make sure you've taken care of the prerequisites (https://docs.apppack.io/setup/#prerequisites).")
fmt.Printf("This process should take less than 10 minutes. After that, you'll be ready to start installing apps on your cluster.\n\n")
alreadyInstalled, err := hasApppackOIDC(sess)
checkErr(err)
if *alreadyInstalled {
fmt.Println("It looks like you've already setup your global AppPack account resources.")
fmt.Printf("Skipping %s\n", aurora.Bold("apppack create account"))
} else {
fmt.Printf("running %s...\n", aurora.White("apppack create account"))
accountCmd.Run(cmd, []string{})
}

fmt.Println("")
alreadyInstalled, err = stackExists(sess, fmt.Sprintf("apppack-region-%s", *sess.Config.Region))
if *alreadyInstalled {
fmt.Printf("It looks like you've already setup the %s region resources.\n", *sess.Config.Region)
fmt.Printf("Skipping %s\n", aurora.Bold("apppack create region"))
} else {
fmt.Printf("running %s...\n", aurora.White("apppack create region"))
createRegionCmd.Run(cmd, []string{})
}

fmt.Println("")
clusterName := cmd.Flags().Lookup("cluster-name").Value.String()
alreadyInstalled, err = stackExists(sess, fmt.Sprintf("apppack-cluster-%s", clusterName))
if *alreadyInstalled {
fmt.Printf("It looks like you've already setup a cluster named %s.\n", clusterName)
fmt.Printf("Skipping %s\n", aurora.Bold(fmt.Sprintf("apppack create cluster %s", clusterName)))
} else {
fmt.Printf("running %s...\n", aurora.White(fmt.Sprintf("apppack create cluster %s", clusterName)))
createClusterCmd.Run(cmd, []string{clusterName})
}

fmt.Println("")
printSuccess("AppPack initialization complete")
fmt.Print("You can now start installing apps onto your cluster.\n")
},
}

func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().StringVar(&region, "region", "", "AWS region to create resources in")
initCmd.Flags().String("cluster-name", "apppack", "name of initial cluster")
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ()
var cfgFile string

const (
timeFmt = "Jan 02, 2006 15:04:05 -0700"
timeFmt = "Jan 02, 2006 15:04:05 -0700"
)

// AppName is used to hold the `--app-name` flag
Expand Down

0 comments on commit 28ef330

Please sign in to comment.