/
create.go
153 lines (125 loc) · 4.72 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package distribute
import (
"fmt"
"os"
"path"
"strings"
"github.com/fatih/color"
"github.com/j3ssie/osmedeus/core"
"github.com/j3ssie/osmedeus/libs"
"github.com/j3ssie/osmedeus/utils"
"github.com/jinzhu/copier"
jsoniter "github.com/json-iterator/go"
)
func (c *CloudRunner) CreateInstance(target string) error {
c.Opt.Cloud.Input = target
c.Target = core.ParseInput(target, c.Opt)
if c.Opt.EnableFormatInput {
c.Opt.Cloud.Input = c.Target["Target"]
}
if c.Opt.Cloud.Workspace == "" {
c.Opt.Cloud.Workspace = utils.CleanPath(c.Target["Target"])
}
if c.Opt.Cloud.Workspace != "" {
c.Target["Workspace"] = c.Opt.Cloud.Workspace
// disable changing workspace name in huntersuite to keep track the scanID
if c.Opt.Cloud.EnableChunk {
if strings.Contains(target, "-chunk-") {
index := strings.Split(target, "-chunk-")[1]
c.Target["WorkspaceChunk"] = path.Base(c.Opt.Cloud.Workspace) + "-" + index
utils.DebugF("Changing workspace name: %v", c.Target["WorkspaceChunk"])
}
}
utils.DebugF(`c.Target["Workspace"] -- %v`, c.Target["Workspace"])
}
// run-flow-example.com
InstancePrefix := fmt.Sprintf("run")
if c.Opt.Cloud.Flow != "" {
InstancePrefix = fmt.Sprintf("run-%s", utils.CleanPath(c.Opt.Cloud.Flow))
}
if c.Opt.Cloud.Module != "" {
InstancePrefix = fmt.Sprintf("run-%s", utils.CleanPath(c.Opt.Cloud.Module))
}
// run-flow-example.com-1
c.InstanceName = fmt.Sprintf("%s-%s", InstancePrefix, strings.TrimSpace(c.Target["Workspace"]))
if c.Opt.Cloud.EnableChunk {
c.InstanceName = fmt.Sprintf("%s-%s", InstancePrefix, strings.TrimSpace(c.Target["WorkspaceChunk"]))
}
// make sure the droplet name is unique
c.InstanceName = c.InstanceName + "-" + utils.RandomString(4)
// clean up the instance name first
if strings.Contains(c.InstanceName, "_") {
c.InstanceName = strings.ReplaceAll(c.InstanceName, "_", "-")
}
// force changing instance name from cli
if c.Opt.Cloud.InstanceName != "" {
c.InstanceName = c.Opt.Cloud.InstanceName
}
// quick check for instance name
switch c.Provider.ProviderName {
case "ln", "line", "linode":
if len(c.InstanceName) > 32 {
c.InstanceName = strings.Trim(strings.Trim(strings.Trim(c.InstanceName[:20], "-"), "."), "_") + "-" + utils.RandomString(9)
}
}
/* Really start to run command to create instance here */
err := c.Provider.CreateInstance(c.InstanceName)
// fix the naming in linode might need another check for length
if err != nil {
// check if account reach limit first
if strings.Contains(err.Error(), "Account Limit reached") {
utils.ErrorF("Account %v reach limit instance", c.Provider.RedactedToken)
return fmt.Errorf("error creating instance")
}
if strings.Contains(err.Error(), "valid hostname characters are allowed") || strings.Contains(err.Error(), "[400] [label]") {
c.InstanceName = fmt.Sprintf("runr-%s-%s", utils.GetTS(), utils.RandomString(8))
}
err = c.Provider.CreateInstance(c.InstanceName)
if err != nil {
return fmt.Errorf("error creating instance")
}
}
if err != nil {
return fmt.Errorf("error creating instance: %v", c.InstanceName)
}
c.PublicIP = c.Provider.CreatedInstance.IPAddress
c.InstanceID = c.Provider.CreatedInstance.InstanceID
c.DestInstance = fmt.Sprintf("%s@%s", c.SSHUser, c.PublicIP)
utils.InforF("Instance created: %s -- %s -- %v", color.HiCyanString(c.InstanceName), color.HiCyanString(c.InstanceID), color.HiCyanString(c.PublicIP))
c.Target["CIP"] = c.PublicIP
c.Target["RemoteIP"] = c.PublicIP
// create the JSON file for the instance
c.WriteInstanceConfig()
return nil
}
func (c *CloudRunner) DeleteInstanceConfig() {
utils.DebugF("Deleting instance config %v", color.HiCyanString(c.InstanceFile))
os.Remove(c.InstanceFile)
}
func (c *CloudRunner) WriteInstanceConfig() error {
instanceFile := c.Opt.Env.InstancesFolder + "/" + c.InstanceName + "-" + c.PublicIP + ".json"
c.InstanceFile = instanceFile
utils.DebugF("Writing instance config to %v", color.HiCyanString(instanceFile))
var instanceObj CloudRunner
copier.Copy(&instanceObj, &c)
instanceObj.Runner = core.Runner{}
instanceObj.Opt = libs.Options{}
if data, err := jsoniter.MarshalToString(instanceObj); err == nil {
utils.WriteToFile(instanceFile, data)
}
return nil
}
func (c *CloudRunner) ErrorWriteInstanceConfig() error {
instanceFile := c.Opt.Env.InstancesFolder + "/" + c.InstanceName + "-" + c.PublicIP + ".json"
c.InstanceFile = instanceFile
utils.DebugF("Writing instance config to %v", color.HiCyanString(instanceFile))
var instanceObj CloudRunner
copier.Copy(&instanceObj, &c)
instanceObj.IsError = true
instanceObj.Runner = core.Runner{}
instanceObj.Opt = libs.Options{}
if data, err := jsoniter.MarshalToString(instanceObj); err == nil {
utils.WriteToFile(instanceFile, data)
}
return nil
}