Skip to content

Commit 99bfc40

Browse files
committed
fix receive command overwriting existing key files and save instance details
1 parent f13aeb7 commit 99bfc40

2 files changed

Lines changed: 58 additions & 14 deletions

File tree

cmd/receive.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,33 @@ var receiveCmd = &cobra.Command{
102102
instanceName = strings.TrimSuffix(fileName, filepath.Ext(fileName))
103103
}
104104

105-
targetPath := filepath.Join(sshDir, fileName)
106-
107-
// 4. Save file with secure permissions
108-
// Open destination file with 0600 permissions (read/write owner)
109-
err = os.WriteFile(targetPath, fileContent, 0600)
110-
if err != nil {
111-
log.Fatalf("Failed to write key file: %v", err)
105+
targetPath := filepath.Join(sshDir, fileName)
106+
107+
// 4. Remove existing file if it exists (it might be read-only)
108+
if _, err := os.Stat(targetPath); err == nil {
109+
// File exists, make it writable first then remove it
110+
os.Chmod(targetPath, 0600)
111+
if err := os.Remove(targetPath); err != nil {
112+
log.Fatalf("Failed to remove existing key file: %v", err)
112113
}
114+
fmt.Printf("Removed existing key file: %s\n", targetPath)
115+
}
113116

114-
// Now lock it down to read-only (0400)
115-
if err := os.Chmod(targetPath, 0400); err != nil {
116-
log.Printf("Warning: failed to set secure permissions (0400): %v", err)
117-
}
117+
// 5. Save file with secure permissions
118+
// Open destination file with 0600 permissions (read/write owner)
119+
err = os.WriteFile(targetPath, fileContent, 0600)
120+
if err != nil {
121+
log.Fatalf("Failed to write key file: %v", err)
122+
}
123+
124+
// Now lock it down to read-only (0400)
125+
if err := os.Chmod(targetPath, 0400); err != nil {
126+
log.Printf("Warning: failed to set secure permissions (0400): %v", err)
127+
}
118128

119129
fmt.Printf("Successfully received key: %s\n", targetPath)
120130

121-
// 5. Save to database
131+
// 6. Save to database
122132
fmt.Println("Adding key to gotoni database...")
123133
database, err := db.InitDB()
124134
if err != nil {
@@ -131,7 +141,7 @@ var receiveCmd = &cobra.Command{
131141
}
132142
}
133143

134-
// 6. Update SSH config (~/.ssh/config)
144+
// 7. Update SSH config (~/.ssh/config)
135145
if instanceIP != "" {
136146
fmt.Printf("Configuring SSH access for %s (%s)...\n", instanceName, instanceIP)
137147
if err := client.UpdateSSHConfig(instanceName, instanceIP, targetPath); err != nil {

pkg/client/lambda_provider.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,47 @@ func (p *LambdaProvider) LaunchAndWait(httpClient *http.Client, apiToken string,
171171
return nil, fmt.Errorf("failed to launch instances: %w", err)
172172
}
173173

174+
// Init DB for updating instance details
175+
database, err := db.InitDB()
176+
if err != nil {
177+
return nil, fmt.Errorf("failed to init db: %w", err)
178+
}
179+
defer database.Close()
180+
174181
// Wait for each instance to become ready
175-
for _, instance := range instances {
182+
for i, instance := range instances {
176183
fmt.Printf("Waiting for instance %s to become ready...\n", instance.ID)
177184
if err := p.WaitForInstanceReady(httpClient, apiToken, instance.ID, timeout); err != nil {
178185
return nil, fmt.Errorf("instance %s failed to become ready: %w", instance.ID, err)
179186
}
180187
fmt.Printf("Instance %s is now ready!\n", instance.ID)
188+
189+
// Get full instance details now that it's ready
190+
instanceDetails, err := p.GetInstance(httpClient, apiToken, instance.ID)
191+
if err != nil {
192+
fmt.Printf("Warning: Failed to get instance details: %v\n", err)
193+
continue
194+
}
195+
196+
// Update database with full details
197+
inst := &db.Instance{
198+
ID: instance.ID,
199+
Name: name,
200+
Region: region,
201+
Status: instanceDetails.Status,
202+
SSHKeyName: instance.SSHKeyName,
203+
FilesystemName: filesystemName,
204+
InstanceType: instanceDetails.InstanceType.Name,
205+
IPAddress: instanceDetails.IP,
206+
}
207+
if err := database.SaveInstance(inst); err != nil {
208+
fmt.Printf("Warning: Failed to update instance in db: %v\n", err)
209+
}
210+
211+
// Update the returned instance with IP
212+
instances[i].ID = instance.ID
213+
instances[i].SSHKeyName = instance.SSHKeyName
214+
instances[i].SSHKeyFile = instance.SSHKeyFile
181215
}
182216

183217
return instances, nil

0 commit comments

Comments
 (0)