Class: Kdeploy::CLI

Inherits:
Thor
  • Object
show all
Extended by:
DSL
Defined in:
lib/kdeploy/cli.rb

Overview

Command-line interface for Kdeploy

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

extended, get_task_hosts, host, inventory, kdeploy_hosts, kdeploy_roles, kdeploy_tasks, role, run, task, upload, upload_template

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/kdeploy/cli.rb', line 14

def self.exit_on_failure?
  true
end

Instance Method Details

#execute(task_file, task_name = nil) ⇒ Object



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
# File 'lib/kdeploy/cli.rb', line 82

def execute(task_file, task_name = nil)
  # 只在最前面输出一次 banner
  @banner_printed ||= false
  unless @banner_printed
    puts Kdeploy::Banner.show
    @banner_printed = true
  end

  load_task_file(task_file)

  tasks_to_run = if task_name
                   [task_name.to_sym]
                 else
                   self.class.kdeploy_tasks.keys
                 end

  tasks_to_run.each do |task|
    task_hosts = self.class.get_task_hosts(task)
    hosts = filter_hosts(options[:limit], task_hosts)

    if hosts.empty?
      puts Kdeploy::Banner.show_error("No hosts found for task: #{task}")
      next
    end

    if options[:dry_run]
      print_dry_run(hosts, task)
      next
    end

    output = ConsoleOutput.new
    runner = Runner.new(hosts, self.class.kdeploy_tasks, parallel: options[:parallel], output: output)
    results = runner.run(task)
    print_results(results, task)
  end
rescue StandardError => e
  puts Kdeploy::Banner.show_error(e.message)
  exit 1
end

#help(command = nil) ⇒ Object



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
# File 'lib/kdeploy/cli.rb', line 27

def help(command = nil)
  if command
    super
  else
    pastel = Pastel.new
    puts Kdeploy::Banner.show
    puts <<~HELP
      #{pastel.bright_white('📖 Available Commands:')}

      #{pastel.bright_yellow('🚀')} #{pastel.bright_white('execute TASK_FILE [TASK]')}     Execute deployment tasks from file
      #{pastel.dim('    --limit HOSTS')}              Limit to specific hosts (comma-separated)
      #{pastel.dim('    --parallel NUM')}             Number of parallel executions (default: 5)
      #{pastel.dim('    --dry-run')}                  Show what would be done without executing

      #{pastel.bright_yellow('🆕')} #{pastel.bright_white('init [DIR]')}                  Initialize new deployment project
      #{pastel.bright_yellow('ℹ️')} #{pastel.bright_white('version')}                    Show version information
      #{pastel.bright_yellow('')} #{pastel.bright_white('help [COMMAND]')}              Show help information

      #{pastel.bright_white('💡 Examples:')}

      #{pastel.dim('# Initialize a new project')}
      #{pastel.bright_cyan('kdeploy init my-deployment')}

      #{pastel.dim('# Deploy to web servers')}
      #{pastel.bright_cyan('kdeploy execute deploy.rb deploy_web')}

      #{pastel.dim('# Backup database')}
      #{pastel.bright_cyan('kdeploy execute deploy.rb backup_db')}

      #{pastel.dim('# Run maintenance on specific hosts')}
      #{pastel.bright_cyan('kdeploy execute deploy.rb maintenance --limit web01')}

      #{pastel.dim('# Preview deployment')}
      #{pastel.bright_cyan('kdeploy execute deploy.rb deploy_web --dry-run')}

      #{pastel.bright_white('📚 Documentation:')}
      #{pastel.bright_cyan('https://github.com/kevin197011/kdeploy')}

    HELP
  end
end

#init(dir = '.') ⇒ Object



70
71
72
73
74
75
76
# File 'lib/kdeploy/cli.rb', line 70

def init(dir = '.')
  initializer = Initializer.new(dir)
  initializer.run
rescue StandardError => e
  puts Kdeploy::Banner.show_error(e.message)
  exit 1
end

#versionObject



22
23
24
# File 'lib/kdeploy/cli.rb', line 22

def version
  puts Kdeploy::Banner.show_version
end