Class: Kdeploy::CLI

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

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)


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

def self.exit_on_failure?
  true
end

Instance Method Details

#execute(task_file, task_name = nil) ⇒ Object



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

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

    runner = Runner.new(hosts, self.class.kdeploy_tasks, parallel: options[:parallel])
    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



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

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



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

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

#versionObject



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

def version
  puts Kdeploy::Banner.show_version
end