This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate.go
130 lines (111 loc) · 3.19 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cmd
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
"gitlab.com/qm64/backpack/pkg"
)
// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create [name]",
Args: cobra.ExactArgs(1),
Short: "Create a new unpacked backpack in a directory",
Long: `Use this command to create the directory structure and some basic files
to modify for new workloads and backpacks. If no value is passed by -d or --dir
a new directory with the name and version of the backapck will be created.
The directory that will be created will have these files:
- backpack.yaml (containing metadata)
- values.yaml (containing the default values for the templates)
- main.nomad (representing a go templates of Nomad Jobs written as HCL)
- README.md (useful documentation)
`,
Run: createRun,
}
func init() {
rootCmd.AddCommand(createCmd)
createCmd.Flags().StringP("dir", "d", "", "specifies the directory to write into")
}
func createRun(cmd *cobra.Command, args []string) {
name := args[0]
b := pkg.Pack{
Name: name,
Version: "0.1.0",
Dependencies: map[string]string{"TODO": "http://backpack.qm64.com/example.backpack"},
DefaultValues: []byte(`# Write the default values for your backpack here
# It is strongly suggested to use inline documentation for quick explanations
# otherwise the *.md files can be used for longer documentation.
datacenters:
- dc1
nginx:
# HTTP is enabled by default
http:
port: 80
# HTTPS is disabled by default
https:
enable: false
port: 443
`),
Templates: pkg.FilesMapType{
"main.nomad": []byte(fmt.Sprintf(`job "%s" {
datacenters = {{ toJson .datacenters }}
type = "service"
group "%s_servers" {
task "nginx" {
driver = "docker"
config {
image = "nginx:alpine"
ports = ["http"{{ if .nginx.https.enable }}, "https" {{ end }}]
}
}
network {
port "http" {
static = {{ .nginx.http.port }}
}
{{- if .nginx.https.enable }}
port "https" {
static = {{ .nginx.https.port }}
}
{{- end }}
}
}
}`, name, name)),
},
Documentation: pkg.FilesMapType{
"README.md": []byte(fmt.Sprintf(`# How to deploy %s
Write here a longer documentation full with links and examples or things to
know when upgrading/downgrading, values combination, etc, etc!
Have fun! 😄
`, name)),
},
}
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
// Checks if a custom directory has been specified, otherwise unpack in
// the backpack name.
directory, _ := cmd.Flags().GetString("dir")
showPath := directory
if directory == "" {
showPath = fmt.Sprintf("%s-%s", b.Name, b.Version)
directory = filepath.Join(cwd, showPath)
err = os.Mkdir(directory, 0744)
if err != nil {
log.Fatalf("Error creating directory: %s", err)
}
}
err = pkg.UnpackInDirectory(&b, directory)
if err != nil {
log.Fatalf("Error unpacking: %s", err)
}
fmt.Printf(`Congratulations! Feel free to modify the files written in %s
You can check that your templates are correct by running
$ backpack run %s --unpacked --debug
When you feel ready to share it with your colleagues and friends you can pack it
by running:
$ backpack pack %s
Happy packing and sharing!
`, directory, showPath, showPath)
}