99 "github.com/BurntSushi/toml"
1010 "github.com/crytic/cloudexec/pkg/config"
1111 do "github.com/crytic/cloudexec/pkg/digitalocean"
12+ "github.com/crytic/cloudexec/pkg/log"
1213 "github.com/crytic/cloudexec/pkg/ssh"
1314 "github.com/crytic/cloudexec/pkg/state"
1415)
@@ -43,7 +44,7 @@ func InitLaunchConfig() error {
4344
4445 // Write the default launch config to the file
4546 _ , err = launchConfigFile .WriteString (`
46- # Set the directory to upload to the droplet .
47+ # Set the directory to upload to the server .
4748[input]
4849directory = ""
4950timeout = "48h"
@@ -83,11 +84,8 @@ func LoadLaunchConfig(launchConfigPath string) (LaunchConfig, error) {
8384 return lc , nil
8485}
8586
86- func Launch (config config.Config , dropletSize string , dropletRegion string , lc LaunchConfig ) error {
87- username := config .Username
88- bucketName := fmt .Sprintf ("cloudexec-%s" , username )
89-
90- // get existing state from bucket
87+ func Launch (config config.Config , serverSize string , serverRegion string , lc LaunchConfig ) error {
88+ // get existing state
9189 existingState , err := state .GetState (config )
9290 if err != nil {
9391 return fmt .Errorf ("Failed to get S3 state: %w" , err )
@@ -100,90 +98,85 @@ func Launch(config config.Config, dropletSize string, dropletRegion string, lc L
10098 } else {
10199 latestJobId = latestJob .ID
102100 }
103- thisJobId := latestJobId + 1
101+ jobID := latestJobId + 1
104102
105103 // update state struct with a new job
106104 newState := & state.State {}
107105 startedAt := time .Now ().Unix ()
108106
109107 newJob := state.Job {
110108 Name : lc .Input .JobName ,
111- ID : thisJobId ,
109+ ID : jobID ,
112110 Status : state .Provisioning ,
113111 StartedAt : startedAt ,
114112 }
115113 newState .CreateJob (newJob )
116114 // sync state to bucket
117- fmt .Printf ("Adding new job to the state...\n " )
118115 err = state .MergeAndSave (config , newState )
116+ log .Info ("Registered new job with id %v" , jobID )
119117 if err != nil {
120118 return fmt .Errorf ("Failed to update S3 state: %w" , err )
121119 }
122120
123121 // upload local files to the bucket
124122 sourcePath := lc .Input .Directory // TODO: verify that this path exists & throw informative error if not
125- destPath := fmt .Sprintf ("job-%v" , thisJobId )
126- fmt .Printf ("Compressing and uploading contents of directory %s to bucket %s/%s...\n " , sourcePath , bucketName , destPath )
123+ destPath := fmt .Sprintf ("job-%v" , jobID )
127124 err = UploadDirectoryToSpaces (config , sourcePath , destPath )
128125 if err != nil {
129126 return fmt .Errorf ("Failed to upload files: %w" , err )
130127 }
131128
132129 // Get or create an SSH key
133- fmt .Println ("Getting or creating SSH key pair..." )
134130 publicKey , err := ssh .GetOrCreateSSHKeyPair ()
135131 if err != nil {
136132 return fmt .Errorf ("Failed to get or creating SSH key pair: %w" , err )
137133 }
138134
139135 // Prepare user data
140- fmt .Println ("Generating user data..." )
141136 userData , err := GenerateUserData (config , lc )
142137 if err != nil {
143138 return fmt .Errorf ("Failed to generate user data: %w" , err )
144139 }
145140
146- fmt . Printf ("Creating new %s droplet in %s for job %d... \n " , dropletSize , config .DigitalOcean .SpacesRegion , thisJobId )
147- droplet , err := do .CreateDroplet (config , config .DigitalOcean .SpacesRegion , dropletSize , userData , thisJobId , publicKey )
141+ log . Wait ("Creating new %s server in %s for job %d" , serverSize , config .DigitalOcean .SpacesRegion , jobID )
142+ server , err := do .CreateDroplet (config , config .DigitalOcean .SpacesRegion , serverSize , userData , jobID , publicKey )
148143 if err != nil {
149- return fmt .Errorf ("Failed to create droplet : %w" , err )
144+ return fmt .Errorf ("Failed to create server : %w" , err )
150145 }
146+ log .Good ("Server created with IP: %v" , server .IP )
151147
152- fmt .Printf ("Droplet created with IP: %v\n " , droplet .IP )
153-
154- // Add the droplet info to state
155- fmt .Println ("Adding new droplet info to state..." )
148+ // Add the server info to state
156149 updatedAt := time .Now ().Unix ()
157150 for i , job := range newState .Jobs {
158- if job .ID == thisJobId {
159- newState .Jobs [i ].Droplet = droplet
151+ if job .ID == jobID {
152+ newState .Jobs [i ].Droplet = server
160153 newState .Jobs [i ].UpdatedAt = updatedAt
161154 }
162155 }
163- fmt .Printf ("Uploading new state to %s\n " , bucketName )
164156 err = state .MergeAndSave (config , newState )
165157 if err != nil {
166158 return fmt .Errorf ("Failed to update S3 state: %w" , err )
167159 }
160+ log .Info ("Saved new server info to state" )
168161
169- // Add the droplet to the SSH config file
170- fmt .Println ("Adding droplet to SSH config file..." )
171- err = ssh .AddSSHConfig (thisJobId , droplet .IP )
162+ // Add the server to the SSH config file
163+ err = ssh .AddSSHConfig (jobID , server .IP )
172164 if err != nil {
173- return fmt .Errorf ("Failed to add droplet to SSH config file: %w" , err )
165+ return fmt .Errorf ("Failed to add server to SSH config file: %w" , err )
174166 }
167+ log .Info ("Added cloudexec-%v to SSH config" , jobID )
175168
176- // Ensure we can SSH into the droplet
177- fmt . Println ( "Ensuring we can SSH into the droplet... " )
178- err = ssh .WaitForSSHConnection (thisJobId )
169+ // Ensure we can SSH into the server
170+ log . Wait ( "Waiting for our new server to wake up " )
171+ err = ssh .WaitForSSHConnection (jobID )
179172 if err != nil {
180- return fmt .Errorf ("Failed to SSH into the droplet : %w" , err )
173+ return fmt .Errorf ("Failed to SSH into the server : %w" , err )
181174 }
182- fmt . Println ( "SSH connection established !" )
183- fmt .Println ("Launch complete" )
184- fmt . Println ( "You can now attach to the running job with: cloudexec attach " )
185- fmt . Println ( "Stream logs from the droplet with: cloudexec logs" )
186- fmt . Println ( "SSH to your droplet with: ssh cloudexec" )
175+ log . Good ( "Good Morning !" )
176+ fmt .Println ()
177+ log . Info ( "Stream logs from the server with: cloudexec logs " )
178+ log . Info ( "SSH to your server with: ssh cloudexec-%v" , jobID )
179+ log . Info ( "Once setup is complete, you can attach to the running job with: cloudexec attach " )
187180
188181 return nil
189182}
0 commit comments