|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "gofr.dev/pkg/gofr" |
| 12 | + |
| 13 | + "kops.dev/internal/models" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + gcp = "GCP" |
| 18 | + executableName = "main" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + errDepKeyNotProvided = errors.New("KOPS_DEPLOYMENT_KEY not provided, please download the key form https://kops.dev") |
| 23 | + errCloudProviderNotRecognized = errors.New("cloud provider in KOPS_DEPLOYMENT_KEY is not provided or supported") |
| 24 | +) |
| 25 | + |
| 26 | +func Deploy(ctx *gofr.Context) (interface{}, error) { |
| 27 | + var deploy models.Deploy |
| 28 | + |
| 29 | + keyFile := os.Getenv("KOPS_DEPLOYMENT_KEY") |
| 30 | + if keyFile == "" { |
| 31 | + return nil, errDepKeyNotProvided |
| 32 | + } |
| 33 | + |
| 34 | + f, err := os.ReadFile(filepath.Clean(keyFile)) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + err = json.Unmarshal(f, &deploy) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + // build binaries for the current working directory |
| 45 | + err = buildBinary() |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + // create and build docker image |
| 51 | + err = createGoDockerFile() |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + tag := ctx.Param("tag") |
| 57 | + if tag == "" { |
| 58 | + tag = "latest" |
| 59 | + } |
| 60 | + |
| 61 | + image := filepath.Clean(deploy.ServiceName + ":" + tag) |
| 62 | + |
| 63 | + err = replaceInputOutput(exec.Command("docker", "build", "-t", image, ".")).Run() |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + // check what cloud provider is |
| 69 | + switch deploy.CloudProvider { |
| 70 | + case gcp: |
| 71 | + err = deployGCP(&deploy, image) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + return "Successfully deployed!", nil |
| 77 | + default: |
| 78 | + return nil, errCloudProviderNotRecognized |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func buildBinary() error { |
| 83 | + fmt.Println("Creating binary for the project") |
| 84 | + |
| 85 | + output, err := exec.Command("sh", "-c", "CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "+executableName+" .").CombinedOutput() |
| 86 | + if err != nil { |
| 87 | + fmt.Println("error occurred while creating binary!", output) |
| 88 | + |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + fmt.Println("Binary created successfully") |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func createGoDockerFile() error { |
| 98 | + content := `FROM alpine:latest |
| 99 | +RUN apk add --no-cache tzdata ca-certificates |
| 100 | +COPY main ./main |
| 101 | +RUN chmod +x /main |
| 102 | +EXPOSE 8000 |
| 103 | +CMD ["/main"]` |
| 104 | + |
| 105 | + fi, _ := os.Stat("Dockerfile") |
| 106 | + if fi != nil { |
| 107 | + fmt.Println("Dockerfile present, using already created dockerfile") |
| 108 | + return nil |
| 109 | + } |
| 110 | + |
| 111 | + file, err := os.Create("Dockerfile") |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + defer file.Close() |
| 117 | + |
| 118 | + if _, err = file.WriteString(content); err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + fmt.Println("Dockerfile created successfully!") |
| 123 | + |
| 124 | + return nil |
| 125 | +} |
0 commit comments