2021-07-15 02:44:07 +00:00
package cli
import (
"fmt"
2021-07-18 03:21:26 +00:00
"coopcloud.tech/abra/client"
"coopcloud.tech/abra/config"
2021-07-19 11:47:46 +00:00
"github.com/sirupsen/logrus"
2021-07-15 02:44:07 +00:00
"github.com/urfave/cli/v2"
)
var serverListCommand = & cli . Command {
Name : "list" ,
Aliases : [ ] string { "ls" } ,
Usage : "List locally-defined servers." ,
ArgsUsage : emptyArgsUsage ,
HideHelp : true ,
2021-07-18 03:21:26 +00:00
Action : func ( c * cli . Context ) error {
dockerContextStore := client . NewDefaultDockerContextStore ( )
contexts , err := dockerContextStore . Store . List ( )
if err != nil {
2021-07-19 11:47:46 +00:00
logrus . Fatal ( err )
2021-07-18 03:21:26 +00:00
}
tableColumns := [ ] string { "Name" , "Connection" }
table := createTable ( tableColumns )
defer table . Render ( )
2021-07-19 11:47:46 +00:00
serverNames , err := config . ReadServerNames ( )
if err != nil {
logrus . Fatal ( err )
}
2021-07-18 03:21:26 +00:00
for _ , serverName := range serverNames {
2021-07-18 05:34:22 +00:00
2021-07-18 03:21:26 +00:00
var row [ ] string
for _ , ctx := range contexts {
2021-07-18 05:34:22 +00:00
endpoint , err := client . GetContextEndpoint ( ctx )
if err != nil {
// This most likely means its a non-docker swarm context and we can ignore
continue
}
2021-07-18 03:21:26 +00:00
if ctx . Name == serverName {
2021-07-18 05:34:22 +00:00
row = [ ] string { serverName , endpoint }
2021-07-18 03:21:26 +00:00
}
}
if len ( row ) == 0 {
row = [ ] string { serverName , "UNKNOWN" }
}
table . Append ( row )
}
return nil
} ,
2021-07-15 02:44:07 +00:00
}
var serverAddCommand = & cli . Command {
Name : "add" ,
Usage : "Add a server, reachable on <host>." ,
ArgsUsage : "<host> [<user>] [<port>]" ,
Description : "[<user>], [<port>] SSH connection details" ,
}
var serverNewCommand = & cli . Command {
Name : "new" ,
Usage : "Creates a VPS from a provider in your terminal!" ,
Description : "Use a provider plugin to create an actual new server resource (VPS or otherwise) which can then be used to house a new Co-op Cloud installation." ,
ArgsUsage : "<provider>" ,
HideHelp : true ,
}
var serverRemoveCommand = & cli . Command {
Name : "remove" ,
Aliases : [ ] string { "rm" } ,
Usage : "Remove server <host>" ,
HideHelp : true ,
}
var serverInitCommand = & cli . Command {
Name : "init" ,
Usage : "Set up a server for Docker swarm" ,
HideHelp : true ,
ArgsUsage : "<host>" ,
Description : ` This initialisation explicitly chooses the "single host swarm" mode
which uses the default IPv4 address as the advertising address . This
can be re - configured later for more advanced use cases .
POWERED BY
docker swarm init
docker network create ... ` ,
}
var serverAppsCommand = & cli . Command {
Name : "apps" ,
Usage : ` Alias for "abra app ls --server <host>" ` ,
HideHelp : true ,
ArgsUsage : " " , // Removes "[arguments]" from help. Empty str's are ignored
Flags : [ ] cli . Flag {
& cli . BoolFlag {
Name : "status" ,
Value : false ,
} ,
} ,
}
// Reminder: The list commands are in is the order they appear in the help menu
var ServerCommand = & cli . Command {
Name : "server" ,
ArgsUsage : "<host>" ,
2021-07-20 22:19:06 +00:00
Usage : "Interact with the servers hosting your apps" ,
2021-07-15 02:44:07 +00:00
HideHelp : true ,
Subcommands : [ ] * cli . Command {
serverNewCommand ,
serverInitCommand ,
serverAddCommand ,
serverListCommand ,
serverRemoveCommand ,
serverAppsCommand ,
} ,
Action : func ( c * cli . Context ) error { fmt . Println ( "server" ) ; return nil } ,
}