Module: Pairhost

Defined in:
lib/pairhost.rb,
lib/pairhost/version.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
"0.0.8"

Class Method Summary collapse

Class Method Details

.configObject



13
14
15
16
17
18
19
20
21
# File 'lib/pairhost.rb', line 13

def self.config
  @config ||= begin
    unless File.exists?(config_file)
      abort "pairhost: No config found. First run 'pairhost init'."
    end
    @credential = Credential.new(config_file)
    YAML.load_file(config_file)
  end
end

.config_fileObject



9
10
11
# File 'lib/pairhost.rb', line 9

def self.config_file
  @config_file ||= File.expand_path('~/.pairhost/config.yml')
end

.connectionObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pairhost.rb', line 31

def self.connection
  return @connection if @connection

  Fog.credentials = Fog.credentials.merge(
    :private_key_path => config['private_key_path'],
  )

  @connection = Fog::Compute.new(
    :provider              => config['provider'],
    :region                => config['region'],
    :aws_secret_access_key => @credential.secret_access_key,
    :aws_access_key_id     => @credential.access_key_id
  )
end

.create(name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pairhost.rb', line 46

def self.create(name)
  server_options = {
    "tags" => {"Name" => name,
               "Created-By-Pairhost-Gem" => VERSION},
    "image_id" => config['ami_id'],
    "flavor_id" => config['flavor_id'],
    "key_name" => config['key_name'],
  }

  server = connection.servers.create(server_options)
  server.wait_for { ready? }

  @instance_id = server.id
  write_instance_id(@instance_id)

  server
end

.fetchObject



87
88
89
90
# File 'lib/pairhost.rb', line 87

def self.fetch
  config
  return instance_id.nil? ? nil : connection.servers.get(instance_id)
end

.fetch!Object



81
82
83
84
85
# File 'lib/pairhost.rb', line 81

def self.fetch!
  server = fetch
  abort "pairhost: No instance found. Please create or attach to one." if server.nil?
  server
end

.instance_idObject



23
24
25
26
27
28
29
# File 'lib/pairhost.rb', line 23

def self.instance_id
  return @instance_id unless @instance_id.nil?

  file = File.expand_path('~/.pairhost/instance')
  @instance_id = File.read(file).chomp if File.exists?(file)
  return @instance_id
end

.start(server) ⇒ Object



71
72
73
74
# File 'lib/pairhost.rb', line 71

def self.start(server)
  server.start
  server.wait_for { ready? }
end

.stop(server) ⇒ Object



76
77
78
79
# File 'lib/pairhost.rb', line 76

def self.stop(server)
  server.stop
  server.wait_for { state == "stopped" }
end

.write_instance_id(instance_id) ⇒ Object



64
65
66
67
68
69
# File 'lib/pairhost.rb', line 64

def self.write_instance_id(instance_id)
  File.open(File.expand_path('~/.pairhost/instance'), "w") do |f|
    f.write(instance_id)
  end
  @instance_id = nil
end