2022-07-04 20:39:34 +00:00
package main
import (
"fmt"
"os"
2022-09-17 16:43:51 +00:00
agent "github.com/kairos-io/kairos/internal/agent"
"github.com/kairos-io/kairos/internal/bus"
2022-07-17 08:42:12 +00:00
2022-12-19 16:01:57 +00:00
"github.com/kairos-io/kairos/pkg/config"
2022-09-17 16:43:51 +00:00
machine "github.com/kairos-io/kairos/pkg/machine"
"github.com/kairos-io/kairos/pkg/utils"
bundles "github.com/kairos-io/kairos/sdk/bundles"
2022-10-23 18:22:32 +00:00
"github.com/kairos-io/kairos/sdk/state"
2022-07-04 20:39:34 +00:00
"github.com/urfave/cli"
)
2022-10-24 11:13:23 +00:00
var configScanDir = [ ] string { "/oem" , "/usr/local/cloud-config" , "/run/initramfs/live" }
2022-07-04 20:39:34 +00:00
var cmds = [ ] cli . Command {
{
Name : "upgrade" ,
Flags : [ ] cli . Flag {
& cli . BoolFlag {
Name : "force" ,
Usage : "Force an upgrade" ,
} ,
2022-08-17 08:31:39 +00:00
& cli . BoolFlag {
Name : "debug" ,
Usage : "Show debug output" ,
} ,
2022-07-04 20:39:34 +00:00
& cli . StringFlag {
Name : "image" ,
Usage : "Specify an full image reference, e.g.: quay.io/some/image:tag" ,
} ,
} ,
Description : `
2022-09-17 16:43:51 +00:00
Manually upgrade a kairos node .
2022-07-04 20:39:34 +00:00
By default takes no arguments , defaulting to latest available release , to specify a version , pass it as argument :
2022-09-17 16:43:51 +00:00
$ kairos upgrade v1 .20 ... .
2022-07-04 20:39:34 +00:00
2022-09-17 16:43:51 +00:00
To retrieve all the available versions , use "kairos upgrade list-releases"
2022-07-04 20:39:34 +00:00
2022-09-17 16:43:51 +00:00
$ kairos upgrade list - releases
2022-07-04 20:39:34 +00:00
2022-09-17 22:11:45 +00:00
See https : //kairos.io/after_install/upgrades/#manual for documentation.
2022-07-04 20:39:34 +00:00
` ,
Subcommands : [ ] cli . Command {
{
Flags : [ ] cli . Flag {
& cli . StringFlag {
Name : "output" ,
Usage : "Output format (json|yaml|terminal)" ,
} ,
} ,
Name : "list-releases" ,
Description : ` List all available releases versions ` ,
Action : func ( c * cli . Context ) error {
2022-08-18 13:12:05 +00:00
releases := agent . ListReleases ( )
2022-08-18 13:14:12 +00:00
releases = utils . ListOutput ( releases , c . String ( "output" ) )
2022-08-18 13:12:05 +00:00
for _ , r := range releases {
fmt . Println ( r )
2022-07-04 20:39:34 +00:00
}
return nil
} ,
} ,
} ,
2022-08-17 08:31:39 +00:00
2022-07-04 20:39:34 +00:00
Action : func ( c * cli . Context ) error {
args := c . Args ( )
var v string
if len ( args ) == 1 {
v = args [ 0 ]
}
2022-10-24 11:13:23 +00:00
return agent . Upgrade ( v , c . String ( "image" ) , c . Bool ( "force" ) , c . Bool ( "debug" ) , configScanDir )
2022-07-04 20:39:34 +00:00
} ,
} ,
2022-08-09 06:01:54 +00:00
{
Name : "notify" ,
Usage : "notify <event> <config dir>..." ,
UsageText : "emits the given event with a generic event payload" ,
Description : `
Sends a generic event payload with the configuration found in the scanned directories .
` ,
Aliases : [ ] string { } ,
Flags : [ ] cli . Flag { } ,
Action : func ( c * cli . Context ) error {
dirs := [ ] string { "/oem" , "/usr/local/cloud-config" }
args := c . Args ( )
if len ( args ) > 1 {
dirs = args [ 1 : ]
}
return agent . Notify ( args [ 0 ] , dirs )
} ,
} ,
2022-07-04 20:39:34 +00:00
{
Name : "start" ,
2022-09-17 16:43:51 +00:00
Usage : "Starts the kairos agent" ,
2022-07-04 20:39:34 +00:00
UsageText : "starts the agent" ,
Description : `
2022-09-17 16:43:51 +00:00
Starts the kairos agent which automatically bootstrap and advertize to the kairos network .
2022-07-04 20:39:34 +00:00
` ,
Aliases : [ ] string { "s" } ,
Flags : [ ] cli . Flag {
2022-08-08 08:15:15 +00:00
& cli . BoolFlag {
Name : "restart" ,
} ,
2022-07-04 20:39:34 +00:00
& cli . BoolFlag {
Name : "force" ,
} ,
& cli . StringFlag {
Name : "api" ,
Value : "http://127.0.0.1:8080" ,
} ,
} ,
Action : func ( c * cli . Context ) error {
dirs := [ ] string { "/oem" , "/usr/local/cloud-config" }
args := c . Args ( )
if len ( args ) > 0 {
dirs = args
}
2022-08-08 08:15:15 +00:00
opts := [ ] agent . Option {
agent . WithAPI ( c . String ( "api" ) ) ,
agent . WithDirectory ( dirs ... ) ,
}
if c . Bool ( "force" ) {
opts = append ( opts , agent . ForceAgent )
}
if c . Bool ( "restart" ) {
opts = append ( opts , agent . RestartAgent )
}
return agent . Run ( opts ... )
2022-07-04 20:39:34 +00:00
} ,
} ,
2022-07-13 22:23:47 +00:00
{
Name : "install-bundle" ,
2022-09-17 16:43:51 +00:00
Usage : "Installs a kairos bundle" ,
2022-07-13 22:23:47 +00:00
Description : `
2022-09-17 16:43:51 +00:00
Manually installs a kairos bundle .
2022-07-13 22:23:47 +00:00
2022-09-17 16:43:51 +00:00
E . g . kairos - agent install - bundle container : quay . io / kairos / kairos ...
2022-07-13 22:23:47 +00:00
` ,
Aliases : [ ] string { "i" } ,
Flags : [ ] cli . Flag {
& cli . StringFlag {
Name : "repository" ,
EnvVar : "REPOSITORY" ,
2022-09-17 16:43:51 +00:00
Value : "docker://quay.io/kairos/packages" ,
2022-07-13 22:23:47 +00:00
} ,
} ,
UsageText : "Install a bundle manually in the node" ,
Action : func ( c * cli . Context ) error {
args := c . Args ( )
if len ( args ) != 1 {
return fmt . Errorf ( "bundle name required" )
}
2022-08-10 16:56:07 +00:00
return bundles . RunBundles ( [ ] bundles . BundleOption { bundles . WithRepository ( c . String ( "repository" ) ) , bundles . WithTarget ( args [ 0 ] ) } )
2022-07-13 22:23:47 +00:00
} ,
} ,
2022-07-04 20:39:34 +00:00
{
Name : "uuid" ,
Usage : "Prints the local UUID" ,
Description : "Print node uuid" ,
Aliases : [ ] string { "u" } ,
Action : func ( c * cli . Context ) error {
fmt . Print ( machine . UUID ( ) )
return nil
} ,
} ,
2022-12-19 16:01:57 +00:00
{
Name : "config" ,
Usage : "get machine configuration" ,
Description : "Print machine state information, e.g. `state get uuid` returns the machine uuid" ,
Aliases : [ ] string { "c" } ,
Action : func ( c * cli . Context ) error {
runtime , err := state . NewRuntime ( )
if err != nil {
return err
}
fmt . Print ( runtime )
return err
} ,
Subcommands : [ ] cli . Command {
{
Name : "show" ,
Usage : "Shows the machine configuration" ,
Description : "Show the runtime configuration of the machine. It will scan the machine for all the configuration and will return the config file processed and found." ,
Aliases : [ ] string { "s" } ,
Action : func ( c * cli . Context ) error {
config , err := config . Scan ( config . Directories ( configScanDir ... ) , config . NoLogs )
if err != nil {
return err
}
2022-10-23 18:22:32 +00:00
2022-12-19 16:01:57 +00:00
fmt . Printf ( "%s" , config . String ( ) )
return nil
} ,
} ,
{
Name : "get" ,
Usage : "Get specific data from the configuration" ,
UsageText : `
Use it to retrieve configuration programmatically from the CLI :
$ kairos - agent config get k3s . enabled
true
or
$ kairos - agent config get k3s
enabled : true ` ,
Description : "It allows to navigate the YAML config file by searching with 'yq' style keywords as `config get k3s` to retrieve the k3s config block" ,
Aliases : [ ] string { "g" } ,
Action : func ( c * cli . Context ) error {
config , err := config . Scan ( config . Directories ( configScanDir ... ) , config . NoLogs )
if err != nil {
return err
}
res , err := config . Query ( c . Args ( ) . First ( ) )
if err != nil {
return err
}
fmt . Printf ( "%s" , res )
return nil
} ,
} ,
} ,
} ,
2022-10-23 18:22:32 +00:00
{
Name : "state" ,
Usage : "get machine state" ,
2022-10-24 06:34:49 +00:00
Description : "Print machine state information, e.g. `state get uuid` returns the machine uuid" ,
2022-10-23 18:22:32 +00:00
Aliases : [ ] string { "s" } ,
Action : func ( c * cli . Context ) error {
runtime , err := state . NewRuntime ( )
if err != nil {
return err
}
fmt . Print ( runtime )
return err
} ,
Subcommands : [ ] cli . Command {
{
Name : "apply" ,
Usage : "Applies a machine state" ,
2022-10-24 06:34:49 +00:00
Description : "Applies machine configuration in runtimes" ,
2022-10-23 18:22:32 +00:00
Aliases : [ ] string { "a" } ,
Action : func ( c * cli . Context ) error {
// TODO
return nil
} ,
} ,
{
Name : "get" ,
Usage : "get specific " ,
Description : "query state data" ,
Aliases : [ ] string { "g" } ,
Action : func ( c * cli . Context ) error {
runtime , err := state . NewRuntime ( )
if err != nil {
return err
}
res , err := runtime . Query ( c . Args ( ) . First ( ) )
fmt . Print ( res )
return err
} ,
} ,
} ,
} ,
2022-07-04 20:39:34 +00:00
{
Name : "interactive-install" ,
Description : `
2022-09-17 16:43:51 +00:00
Starts kairos in interactive mode install .
2022-07-04 20:39:34 +00:00
2022-08-12 13:49:41 +00:00
It will ask prompt for several questions and perform an install depending on the providers available in the system .
2022-07-04 20:39:34 +00:00
2022-09-17 22:11:45 +00:00
See also https : //kairos.io/installation/interactive_install/ for documentation.
2022-07-04 20:39:34 +00:00
2022-08-12 13:49:41 +00:00
This command is meant to be used from the boot GRUB menu , but can be also started manually ` ,
2022-07-04 20:39:34 +00:00
Flags : [ ] cli . Flag {
& cli . BoolFlag {
Name : "shell" ,
} ,
} ,
Usage : "Starts interactive installation" ,
Action : func ( c * cli . Context ) error {
2022-07-17 08:42:12 +00:00
return agent . InteractiveInstall ( c . Bool ( "shell" ) )
2022-07-04 20:39:34 +00:00
} ,
} ,
2022-09-10 13:01:03 +00:00
{
Name : "manual-install" ,
Usage : "Starts the manual installation" ,
Description : `
` ,
Aliases : [ ] string { "m" } ,
Flags : [ ] cli . Flag {
& cli . StringFlag {
Name : "device" ,
} ,
& cli . BoolFlag {
Name : "poweroff" ,
} ,
& cli . BoolFlag {
Name : "reboot" ,
} ,
} ,
Action : func ( c * cli . Context ) error {
if c . NArg ( ) == 0 {
return fmt . Errorf ( "expect one argument. the config file - if you don't have it, use the interactive-install" )
}
config := c . Args ( ) . First ( )
options := map [ string ] string { "device" : c . String ( "device" ) }
if c . Bool ( "poweroff" ) {
options [ "poweroff" ] = "true"
}
if c . Bool ( "reboot" ) {
options [ "reboot" ] = "true"
}
return agent . ManualInstall ( config , options )
} ,
} ,
2022-07-04 20:39:34 +00:00
{
Name : "install" ,
2022-09-17 16:43:51 +00:00
Usage : "Starts the kairos pairing installation" ,
2022-07-04 20:39:34 +00:00
Description : `
2022-09-17 16:43:51 +00:00
Starts kairos in pairing mode .
2022-07-04 20:39:34 +00:00
2022-09-17 16:43:51 +00:00
It will print out a QR code which can be used with "kairos register" to send over a configuration and bootstraping a kairos node .
2022-07-04 20:39:34 +00:00
2022-09-17 22:11:45 +00:00
See also https : //kairos.io/installation/device_pairing/ for documentation.
2022-07-04 20:39:34 +00:00
This command is meant to be used from the boot GRUB menu , but can be started manually ` ,
Aliases : [ ] string { "i" } ,
Action : func ( c * cli . Context ) error {
2022-10-24 11:13:23 +00:00
return agent . Install ( configScanDir ... )
2022-07-04 20:39:34 +00:00
} ,
} ,
{
Name : "recovery" ,
Aliases : [ ] string { "r" } ,
2022-07-18 22:02:49 +00:00
Action : func ( c * cli . Context ) error {
return agent . Recovery ( )
} ,
2022-09-17 16:43:51 +00:00
Usage : "Starts kairos recovery mode" ,
2022-07-04 20:39:34 +00:00
Description : `
2022-09-17 16:43:51 +00:00
Starts kairos recovery mode .
2022-07-04 20:39:34 +00:00
2022-09-17 16:43:51 +00:00
In recovery mode a QR code will be printed out on the screen which should be used in conjunction with "kairos bridge" . Pass by the QR code as snapshot
to the bridge to connect over the machine which runs the "kairos recovery" command .
2022-07-04 20:39:34 +00:00
2022-09-17 22:11:45 +00:00
See also https : //kairos.io/after_install/recovery_mode/ for documentation.
2022-07-04 20:39:34 +00:00
This command is meant to be used from the boot GRUB menu , but can likely be used standalone ` ,
} ,
{
2022-07-18 22:02:49 +00:00
Name : "reset" ,
Action : func ( c * cli . Context ) error {
2022-10-24 11:13:23 +00:00
return agent . Reset ( configScanDir ... )
2022-07-18 22:02:49 +00:00
} ,
2022-09-17 16:43:51 +00:00
Usage : "Starts kairos reset mode" ,
2022-07-04 20:39:34 +00:00
Description : `
2022-09-17 16:43:51 +00:00
Starts kairos reset mode , it will nuke completely the node data and restart fresh .
2022-07-04 20:39:34 +00:00
Attention ! this will delete any persistent data on the node . It is equivalent to re - init the node right after the installation .
In reset mode a the node will automatically reset
2022-09-17 22:11:45 +00:00
See also https : //kairos.io/after_install/reset_mode/ for documentation.
2022-07-04 20:39:34 +00:00
This command is meant to be used from the boot GRUB menu , but can likely be used standalone ` ,
} ,
}
func main ( ) {
bus . Manager . Initialize ( )
app := & cli . App {
2022-09-17 16:43:51 +00:00
Name : "kairos-agent" ,
2022-07-04 20:39:34 +00:00
Version : "0.1" ,
Author : "Ettore Di Giacinto" ,
2022-09-17 16:43:51 +00:00
Usage : "kairos agent start" ,
2022-07-04 20:39:34 +00:00
Description : `
2022-09-17 16:43:51 +00:00
The kairos agent is a component to abstract away node ops , providing a common feature - set across kairos variants .
2022-07-04 20:39:34 +00:00
` ,
UsageText : ` ` ,
2022-09-17 16:43:51 +00:00
Copyright : "kairos authors" ,
2022-07-04 20:39:34 +00:00
2022-08-11 11:31:00 +00:00
Commands : cmds ,
2022-07-04 20:39:34 +00:00
}
err := app . Run ( os . Args )
if err != nil {
fmt . Println ( err )
os . Exit ( 1 )
}
}