Skip to content

Commit

Permalink
initial draft for pulumi supporting mulitple providers
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianriobo committed Jan 23, 2023
1 parent 22e466b commit 1c5254c
Show file tree
Hide file tree
Showing 2,664 changed files with 885,991 additions and 0 deletions.
1 change: 1 addition & 0 deletions pulumi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out
71 changes: 71 additions & 0 deletions pulumi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
VERSION ?= 0.0.1
CONTAINER_MANAGER ?= podman
# Image URL to use all building/pushing image targets
IMG ?= quay.io/crcont/crc-cloud:${VERSION}

# Go and compilation related variables
GOPATH ?= $(shell go env GOPATH)
BUILD_DIR ?= out
SOURCE_DIRS = cmd pkg test
# https://golang.org/cmd/link/
LDFLAGS := $(VERSION_VARIABLES) -extldflags='-static' ${GO_EXTRA_LDFLAGS}
GCFLAGS := all=-N -l

# Add default target
.PHONY: default
default: install

# Create and update the vendor directory
.PHONY: vendor
vendor:
go mod tidy
go mod vendor

.PHONY: check
check: build test lint

# Start of the actual build targets

.PHONY: install
install: $(SOURCES)
go install -ldflags="$(LDFLAGS)" $(GO_EXTRA_BUILDFLAGS) ./cmd

$(BUILD_DIR)/crc-cloud: $(SOURCES)
GOOS=linux GOARCH=amd64 go build -gcflags="$(GCFLAGS)" -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/crc-cloud $(GO_EXTRA_BUILDFLAGS) ./cmd



.PHONY: build
build: clean $(BUILD_DIR)/crc-cloud

.PHONY: test
test:
go test -race --tags build -v -ldflags="$(VERSION_VARIABLES)" ./pkg/... ./cmd/...

.PHONY: clean ## Remove all build artifacts
clean:
rm -rf $(BUILD_DIR)
rm -f $(GOPATH)/bin/crc-cloud

.PHONY: fmt
fmt:
@gofmt -l -w $(SOURCE_DIRS)

$(GOPATH)/bin/golangci-lint:
go install github.com/golangci/golangci-lint/cmd/[email protected]

# Run golangci-lint against code
.PHONY: lint
lint: $(GOPATH)/bin/golangci-lint
$(GOPATH)/bin/golangci-lint run

# Build the container image
.PHONY: oci-build
oci-build:
${CONTAINER_MANAGER} build -t ${IMG} -f oci/Containerfile .

# Push the container image
.PHONY: oci-push
container-push:
${CONTAINER_MANAGER} push ${IMG}

21 changes: 21 additions & 0 deletions pulumi/cmd/cmd/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

const (
projectName string = "project-name"
projectNameDesc string = "project name to identify the instance of the stack"
backedURL string = "backed-url"
backedURLDesc string = "backed for stack state. Can be a local path with format file:///path/subpath or s3 s3://existing-bucket"
outputFolder string = "output"
outputFolderDesc string = "path to export assets"
provider string = "provider"
providerDesc string = "target cloud provider"
ocpPullSecretFilePath string = "pullsecret-filepath"
ocpPullSecretFilePathDesc string = "path for pullsecret file"
keyFilePath string = "key-filepath"
keyFilePathDesc string = "path to init key obtained when importing the image"

bundleDownloadURL string = "bundle-url"
bundleDownloadURLDesc string = "custom url to download the bundle artifact"
shasumfileDownloadURL string = "bundle-shasumfile-url"
shasumfileDownloadURLDesc string = "custom url to download the shasum file to verify the bundle artifact"
)
58 changes: 58 additions & 0 deletions pulumi/cmd/cmd/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"os"

"github.com/crc/crc-cloud/pkg/manager"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
createCmdName string = "create"
createCmdDescription string = "create crc cloud instance"
)

func init() {
rootCmd.AddCommand(crcCloudCreateCmd)
flagSet := pflag.NewFlagSet(createCmdName, pflag.ExitOnError)
// Fixed params
flagSet.StringP(projectName, "", "", projectNameDesc)
flagSet.StringP(backedURL, "", "", backedURLDesc)
flagSet.StringP(outputFolder, "", "", outputFolderDesc)
flagSet.StringP(provider, "", "", providerDesc)
flagSet.StringP(ocpPullSecretFilePath, "", "", ocpPullSecretFilePathDesc)
flagSet.StringP(keyFilePath, "", "", keyFilePathDesc)
// Provider dependent params
for name, description := range manager.CreateParams() {
flagSet.StringP(name, "", "", description)
}
crcCloudCreateCmd.Flags().AddFlagSet(flagSet)
}

var crcCloudCreateCmd = &cobra.Command{
Use: createCmdName,
Short: createCmdDescription,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
// Provider dependet params
providerParams := make(map[string]string)
for name := range manager.CreateParams() {
providerParams[name] = viper.GetString(name)
}
if err := manager.Create(
viper.GetString(projectName),
viper.GetString(backedURL),
viper.GetString(outputFolder),
viper.GetString(provider),
providerParams,
viper.GetString(ocpPullSecretFilePath),
viper.GetString(keyFilePath)); err != nil {
os.Exit(1)
}
return nil
},
}
41 changes: 41 additions & 0 deletions pulumi/cmd/cmd/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"os"

"github.com/crc/crc-cloud/pkg/manager"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
destroyCmdName string = "destroy"
destroyCmdDescription string = "destroy crc cloud instance"
)

func init() {
rootCmd.AddCommand(crcCloudDestroyCmd)
flagSet := pflag.NewFlagSet(createCmdName, pflag.ExitOnError)
flagSet.StringP(projectName, "", "", projectNameDesc)
flagSet.StringP(backedURL, "", "", backedURLDesc)
flagSet.StringP(provider, "", "", providerDesc)
crcCloudDestroyCmd.Flags().AddFlagSet(flagSet)
}

var crcCloudDestroyCmd = &cobra.Command{
Use: destroyCmdName,
Short: destroyCmdDescription,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
if err := manager.Destroy(
viper.GetString(projectName),
viper.GetString(backedURL),
viper.GetString(provider)); err != nil {
os.Exit(1)
}
return nil
},
}
48 changes: 48 additions & 0 deletions pulumi/cmd/cmd/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"os"

"github.com/crc/crc-cloud/pkg/manager"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
importImageCmdName string = "import"
importImageCmdDescription string = "import crc cloud image"
)

func init() {
rootCmd.AddCommand(crcCloudImportCmd)
flagSet := pflag.NewFlagSet(importImageCmdName, pflag.ExitOnError)
// Fixed params
flagSet.StringP(projectName, "", "", projectNameDesc)
flagSet.StringP(backedURL, "", "", backedURLDesc)
flagSet.StringP(provider, "", "", providerDesc)
flagSet.StringP(outputFolder, "", "", outputFolderDesc)
flagSet.StringP(bundleDownloadURL, "", "", bundleDownloadURLDesc)
flagSet.StringP(shasumfileDownloadURL, "", "", shasumfileDownloadURLDesc)
crcCloudImportCmd.Flags().AddFlagSet(flagSet)
}

var crcCloudImportCmd = &cobra.Command{
Use: importImageCmdName,
Short: importImageCmdDescription,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
if err := manager.Import(
viper.GetString(projectName),
viper.GetString(backedURL),
viper.GetString(provider),
viper.GetString(outputFolder),
viper.GetString(bundleDownloadURL),
viper.GetString(shasumfileDownloadURL)); err != nil {
os.Exit(1)
}
return nil
},
}
73 changes: 73 additions & 0 deletions pulumi/cmd/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)

const (
commandName = "crc-cloud"
descriptionShort = "PoC for pulumi"
descriptionLong = "PoC for pulumi"

defaultErrorExitCode = 1
)

var rootCmd = &cobra.Command{
Use: commandName,
Short: descriptionShort,
Long: descriptionLong,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return runPrerun(cmd)
},
Run: func(cmd *cobra.Command, args []string) {
runRoot()
_ = cmd.Help()
},
SilenceUsage: true,
SilenceErrors: true,
}

func runPrerun(cmd *cobra.Command) error {
return nil
}

func runRoot() {
fmt.Println("No command given")
}

func Execute() {
attachMiddleware([]string{}, rootCmd)

if err := rootCmd.ExecuteContext(context.Background()); err != nil {
runPostrun()
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(defaultErrorExitCode)
}
runPostrun()
}

func attachMiddleware(names []string, cmd *cobra.Command) {
if cmd.HasSubCommands() {
for _, command := range cmd.Commands() {
attachMiddleware(append(names, cmd.Name()), command)
}
} else if cmd.RunE != nil {
fullCmd := strings.Join(append(names, cmd.Name()), " ")
src := cmd.RunE
cmd.RunE = executeWithLogging(fullCmd, src)
}
}

func executeWithLogging(fullCmd string, input func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
return input(cmd, args)
}
}

func runPostrun() {
}
7 changes: 7 additions & 0 deletions pulumi/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/crc/crc-cloud/cmd/cmd"

func main() {
cmd.Execute()
}
1 change: 1 addition & 0 deletions pulumi/docs/crc-cloud.drawio

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pulumi/docs/crc-cloud.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1c5254c

Please sign in to comment.