Class: Pec::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/pec/cli.rb

Instance Method Summary collapse

Instance Method Details

#destroy(name = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pec/cli.rb', line 52

def destroy(name = nil)
  config = Pec::Configure.new
  config.load("Pec.yaml")
  config.each do |host|
    next if !name.nil? && host.name != name
    begin
      Pec::Compute::Server.new.destroy!(host.name) if options[:force] || yes?("#{host.name}: Are you sure you want to destroy the '#{host.name}' VM? [y/N]")
    rescue Pec::Errors::Error => e
      puts e
      puts "can't create server:#{host.name}"
    end
  end if config
  rescue Errno::ENOENT => e
    puts e
end

#init ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pec/cli.rb', line 7

def init
  dirname = "user_datas"
  unless FileTest.exist?(dirname)
    FileUtils.mkdir_p(dirname) 
    puts "create directry user_datas"
  end
  unless File.exist?("Pec.yaml")
    open("Pec.yaml","w") do |e|
      YAML.dump(Pec::Configure::Sample.pec_file, e)
    end
    puts "create configure file Pec.yaml"
  end
  open("#{dirname}/web_server.yaml.sample","w") do |e|
    YAML.dump(Pec::Configure::Sample.user_data, e)
  end if FileTest.exist?(dirname)

end

#up(host_name = 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
# File 'lib/pec/cli.rb', line 26

def up(host_name = nil)
  config = Pec::Configure.new
  config.load("Pec.yaml")

  director = Pec::VmDirector.new

  config.each do |host|
    next if !host_name.nil? && host.name != host_name

    begin
      director.make(host)
    rescue Pec::Errors::Error => e
      puts e
      puts "can't create server:#{host.name}"
    rescue Excon::Errors::Error => e
      JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
    end
  end if config
  rescue Errno::ENOENT => e
    puts e
  rescue Pec::Errors::Configure => e
    puts "configure can't load"
end