/
deploy.go
131 lines (109 loc) · 4.23 KB
/
deploy.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
131
/*
Package cmd includes all of the Create Go App CLI commands.
Create a new production-ready project with backend (Golang),
frontend (JavaScript, TypeScript) and deploy automation
(Ansible, Docker) by running one CLI command.
-> Focus on writing code and thinking of business logic!
<- The Create Go App CLI will take care of the rest.
Copyright © 2019-present Vic Shóstak <truewebartisans@gmail.com> (https://1wa.co)
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 (
"os"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/create-go-app/cli/pkg/cgapp"
"github.com/create-go-app/cli/pkg/registry"
"github.com/spf13/cobra"
)
// deployCmd represents the `deploy` command.
var deployCmd = &cobra.Command{
Use: "deploy",
Aliases: []string{"serve"},
Short: "Deploy your project to a remote server via interactive UI or configuration file",
Long: "\nDeploy your project to a remote server via interactive UI or configuration file.",
Run: runDeployCmd,
}
// runDeployCmd represents runner for the `deploy` command.
var runDeployCmd = func(cmd *cobra.Command, args []string) {
// Start message.
cgapp.SendMsg(true, "* * *", "Deploying project via Create Go App CLI v"+registry.CLIVersion+"...", "yellow", true)
// If config is set and correct, skip survey and use it.
if useConfigFile && rolesConfig != nil {
// Re-define variables from config file (default is $PWD/.cgapp.yml).
username = strings.ToLower(rolesConfig["username"].(string))
host = strings.ToLower(rolesConfig["host"].(string))
network = strings.ToLower(rolesConfig["network"].(string))
askBecomePass = rolesConfig["become"].(bool)
} else {
// Start survey.
if err := survey.Ask(
registry.DeployQuestions, &deployAnswers, survey.WithIcons(surveyIconsConfig),
); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
// If something went wrong, cancel and exit.
if !deployAnswers.AgreeDeployment {
cgapp.SendMsg(true, "[!]", "You're stopped deployment process of your project.", "red", false)
cgapp.SendMsg(false, "[!]", "Run `cgapp deploy` once again!", "red", true)
os.Exit(1)
}
// Insert empty line.
cgapp.SendMsg(false, "", "", "", false)
// Define variables for better display.
username = deployAnswers.Username
host = deployAnswers.Host
network = deployAnswers.Network
askBecomePass = deployAnswers.AskBecomePass
}
// Start timer.
startTimer := time.Now()
// Create config files for your project.
cgapp.SendMsg(false, "*", "Run Ansible playbook `"+playbook+"`...", "cyan", true)
// Define Ansible options.
options := []string{
playbook,
"-u", username,
"-e", "host=" + host + " network_name=" + network,
}
// Check, if need to ask password for username.
// See: https://docs.ansible.com/ansible/latest/user_guide/become.html#become-command-line-options
if askBecomePass {
options = []string{
playbook,
"-u", username,
"-e", "host=" + host + " network_name=" + network,
"--ask-become-pass",
}
}
// Run execution for Ansible playbook.
if err := cgapp.ExecCommand("ansible-playbook", options); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
// Stop timer.
stopTimer := time.Since(startTimer).String()
// End message.
cgapp.SendMsg(true, "* * *", "Completed in "+stopTimer+"!", "yellow", true)
cgapp.SendMsg(false, "(i)", "A helpful documentation and next steps -> https://create-go.app/", "green", false)
cgapp.SendMsg(false, "(i)", "Go to the `"+host+"` to see your deployed project! :)", "green", true)
}
func init() {
rootCmd.AddCommand(deployCmd)
deployCmd.PersistentFlags().BoolVarP(
&useConfigFile,
"use-config", "c", false,
"use config file to create a new project or deploy to a remote server (default is $PWD/.cgapp.yml)",
)
}