Module: Pairhost

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

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.configObject



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

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

.config_fileObject



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

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

.connectionObject



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

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'],
    :aws_secret_access_key => config['aws_secret_access_key'],
    :aws_access_key_id     => config['aws_access_key_id'],
  )
end

.create(name) ⇒ Object



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

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



84
85
86
87
# File 'lib/pairhost.rb', line 84

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

.fetch!Object



78
79
80
81
82
# File 'lib/pairhost.rb', line 78

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

.instance_idObject



21
22
23
24
25
26
27
# File 'lib/pairhost.rb', line 21

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



68
69
70
71
# File 'lib/pairhost.rb', line 68

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

.stop(server) ⇒ Object



73
74
75
76
# File 'lib/pairhost.rb', line 73

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

.write_instance_id(instance_id) ⇒ Object



61
62
63
64
65
66
# File 'lib/pairhost.rb', line 61

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