-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudfile_tools.rb
executable file
·163 lines (138 loc) · 5.13 KB
/
cloudfile_tools.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env ruby
require 'rubygems'
require 'thor'
require 'fog'
require 'pp'
class CloudfileTools < Thor
## server params
#@rackspace_username = ARGV
#@rackspace_api_key = ARGV
#@flavor = ""
#@image = ""
#@name = ""
#@key_private = ""
#@key_public = ""
class_option :user, :aliases => "-u", :desc => "rackspace username"
class_option :key, :aliases => "-k", :desc => "rackspace API key"
class_option :config_file, :default => "./config.yml", :aliases => "-c", :desc => "file of configuration options"
no_tasks do
def connect
if options['user'] && options['key']
# assemble hash of rackspace api credentials
@@rackspace_credentials = {
:provider => 'Rackspace',
:rackspace_username => options['user'],
:rackspace_api_key => options['key']
}
else
exit
end
end
end
##
## start command
##
desc "start", "start server build"
## command options
method_option :image_id, :required => true, :aliases => "-i", :desc => "which server image to use"
method_option :flavor_id, :required => true, :aliases => "-f", :desc => "server flavor to use"
method_option :name, :required => true, :aliases => "-n", :desc => "server name to use"
def start
# code to build server
puts "rackspace server build"
puts "===================="
puts "STARTING....."
compute = authenticate(options[:config_file])
server = compute.servers.create(
:image_id => options['image_id'],
:flavor_id => options['flavor_id'],
:name => options['name']
)
puts "NEW SERVER INFO"
puts "===================="
puts "building new server.....please wait"
pp server
puts "address: " + server.public_ip_address
puts "root: " + server.password
server.wait_for { ready? }
server.private_key = IO.read( File.expand_path('~/.ssh/id_rsa') )
server.public_key = IO.read( File.expand_path('~/.ssh/id_rsa.pub') )
server.username = 'root'
server.setup :password => server.password
#server.ssh ["echo '#{user_data}' > /root/bootstrap.sh", "chmod +x /root/bootstrap.sh", "/root/bootstrap.sh &"]
# create a named user for deploys, add them to the user group, set default password
server.ssh ["groupadd web", "useradd #{@@config['newuser_name']} -g web", "echo #{@@config['newuser_passwd']} | passwd #{@@config['newuser_name']} --stdin;"]
server.ssh ["mkdir /home/#{@@config['newuser_name']}/.ssh", "chmod 700 /home/#{@@config['newuser_name']}/.ssh", "chown #{@@config['newuser_name']}.web /home/#{@@config['newuser_name']}/.ssh"]
puts "===================="
puts "SERVER READY"
puts "===================="
end
##
## list containers
##
method_option :region, :aliases => "-r", :desc => "region"
desc "list_containers", "list containers"
def list_containers
puts "list_containers"
puts "===================="
storage = authenticate(options[:config_file], options[:region])
pp storage.directories
end
##
## list files
##
method_option :region, :required => true, :aliases => "-r", :desc => "region"
method_option :container, :required => true, :aliases => "-c", :desc => "container"
desc "list_files", "list files in container"
def list_files
puts "list_files"
puts "===================="
storage = authenticate(options[:config_file], options[:region])
directory = storage.directories.get(options["container"])
directory.files.each do |file|
pp file.key
end
end
##
## put file
##
desc "put_file", "save file to container"
def put_file
puts "put file"
puts "===================="
# connect
storage = authenticate(options[:config_file])
# open container
directory = storage.directories.get(options[:container])
# check file exists
File.open(options[:file], "rb") do |io|
directory.files.create(:key => File.basename(options[:file]),
:body => io)
end
#File.open(options[:file])
# get file and send to cloudfiles
#file = directory.files.create(
# :key => File.basename(options[:file]),
# :body => File.open(options[:file])
#)
end
private
def authenticate(file_name, region = '', service_net = false)
# load the configuration file with connection parameters
@@config ||= YAML.load_file(file_name)
# init the databasedotcom gem with the specified yml config file
if @@config['user'] && @@config['key']
rackspace_credentials = {
:provider => 'Rackspace',
:rackspace_username => @@config['user'],
:rackspace_api_key => @@config['key']
}
end
rackspace_credentials.merge!({:rackspace_region => region.downcase.to_sym}) if !region.nil?
rackspace_credentials.merge!({:rackspace_servicenet => service_net}) if !service_net.nil?
pp rackspace_credentials
client = Fog::Storage.new(rackspace_credentials)
return client
end
end
CloudfileTools.start