Class: Phper::Commands
- Inherits:
-
CommandLineUtils::Commands
- Object
- CommandLineUtils::Commands
- Phper::Commands
- Includes:
- Phper
- Defined in:
- lib/phper/commands.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
Instance Method Summary collapse
- #create ⇒ Object
- #db_init ⇒ Object
- #deploy ⇒ Object
- #destroy ⇒ Object
- #files ⇒ Object
- #files_dump ⇒ Object
- #files_get ⇒ Object
- #files_modified ⇒ Object
- #files_modified_get ⇒ Object
- #homedir ⇒ Object
- #hosts ⇒ Object
- #info ⇒ Object
-
#initialize {|_self| ... } ⇒ Commands
constructor
A new instance of Commands.
- #keys ⇒ Object
- #keys_add ⇒ Object
- #keys_clear ⇒ Object
- #keys_remove ⇒ Object
- #list ⇒ Object
- #login ⇒ Object
- #logout ⇒ Object
- #open ⇒ Object
- #servers ⇒ Object
- #servers_add ⇒ Object
- #servers_clear ⇒ Object
- #servers_remove ⇒ Object
Methods included from Phper
#decode, #encode, #git_remote, #git_remotes, #git_root, #in_git?, #name_of_key
Constructor Details
#initialize {|_self| ... } ⇒ Commands
Returns a new instance of Commands.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/phper/commands.rb', line 11 def initialize super @commands += ["login","logout","list","create","destroy","info"] @commands += ["keys","keys:add","keys:remove","keys:clear"] @commands += ["servers","servers:add","servers:remove"] @commands += ["open","db:init","deploy"] @commands += ["hosts"] @commands += ["files","files:dump","files:get"] @commands += ["files:modified","files:modified:get"] @commands += ["logs","logs:tail"] @commands += ["versions","versions:set"] @agent = Agent.new # @cache_file = homedir + "/.phper.cache" # @cache = Cache.new(@cache_file) yield self if block_given? end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
10 11 12 |
# File 'lib/phper/commands.rb', line 10 def commands @commands end |
Instance Method Details
#create ⇒ Object
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 |
# File 'lib/phper/commands.rb', line 106 def create OptionParser.new { |opt| opt.parse!(@command_options) @summery = "craete project" @banner = "[<name>]" return opt if @help } name = @command_options.shift start project = @agent.projects_create name puts "Created %s" % project["project"]["id"] puts "--> %s" % project["project"]["git"] puts "--> mysql://%s:%s@%s/%s" % [project["project"]["dbuser"], project["project"]["dbpassword"], "db.phper.jp", project["project"]["dbname"]] # if here if in_git? and project["project"]["id"] != git_remote(Dir.pwd) git = project["project"]["git"] exec_report("git remote add phper #{git}") init_phper_dir end end |
#db_init ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/phper/commands.rb', line 334 def db_init project = nil OptionParser.new { |opt| project = extract_project(opt) @banner = "" @summery = "Initialize project database." return opt if @help } raise "project is not specified." unless project start @agent.projects_init_db(project) project = @agent.projects(project) puts "Initialize Database %s" % project["project"]["id"] puts "--> mysql://%s:%s@%s/%s" % [project["project"]["dbuser"], project["project"]["dbpassword"], "db.phper.jp", project["project"]["dbname"]] end |
#deploy ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/phper/commands.rb', line 320 def deploy project = nil OptionParser.new { |opt| project = extract_project(opt) @banner = "" @summery = "Deploy project" return opt if @help } raise "project is not specified." unless project start @agent.projects_deploy(project) puts "Deploy #{project}" end |
#destroy ⇒ Object
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 |
# File 'lib/phper/commands.rb', line 132 def destroy project = nil yes = false OptionParser.new { |opt| opt.on('-y','--yes', 'answer yes for all questions') { |v| yes = true } project = extract_project(opt) @summery = "destroy project" return opt if @help } raise "project is not specified." unless project start pj = @agent.projects(project) git = pj["project"]["git"] raise "project is not exist." unless pj yes = true if yes or ask("Destroy #{project}? ",["yes","no"]) == "yes" if yes @agent.projects_delete project puts "Destroyed #{project}" # if here if in_git? and project == git_remote(Dir.pwd) git_remotes(git).each{ |name| exec_report("git remote rm #{name}") } end end end |
#files ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/phper/commands.rb', line 368 def files project = nil params = {} OptionParser.new { |opt| opt.on('-h HOST','--host=HOST', 'host') { |v| params[:host] = v } project = extract_project(opt) @summery = "list project files" return opt if @help } raise "project is not specified." unless project start unless params[:host] host = @agent.hosts(project).first params[:host] = host["host"]["id"] end raise "project has no hosts" unless params[:host] @agent.files(project,params[:host]).each { |file| puts "%s" % [file["file"]["name"]] } end |
#files_dump ⇒ Object
399 400 401 402 403 404 |
# File 'lib/phper/commands.rb', line 399 def files_dump @summery = "dump file." return files_get_one if @help file = files_get_one puts Base64.decode64(file["file"]["contents"]) end |
#files_get ⇒ Object
392 393 394 395 396 397 |
# File 'lib/phper/commands.rb', line 392 def files_get @summery = "get and put file." return files_get_one if @help file = files_get_one file_put(file) end |
#files_modified ⇒ Object
406 407 408 409 410 411 412 |
# File 'lib/phper/commands.rb', line 406 def files_modified @summery = "list modified files since last deploy." return file_get_modified if @help file_get_modified.each { |file| puts "%s" % [file["file"]["name"]] } end |
#files_modified_get ⇒ Object
414 415 416 417 418 419 420 421 |
# File 'lib/phper/commands.rb', line 414 def files_modified_get @summery = "get modified files since last deploy." return file_get_modified if @help file_get_modified.each { |file| file = @agent.files(@project,@params[:host],file["file"]["name"]) file_put(file) } end |
#homedir ⇒ Object
29 30 31 |
# File 'lib/phper/commands.rb', line 29 def homedir ENV['HOME'] end |
#hosts ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/phper/commands.rb', line 353 def hosts project = nil OptionParser.new { |opt| project = extract_project(opt) @summery = "list project hosts" return opt if @help } raise "project is not specified." unless project start @agent.hosts(project).each { |host| puts "%s" % [host["host"]["id"]] } end |
#info ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/phper/commands.rb', line 85 def info project = nil OptionParser.new { |opt| project = extract_project(opt) @summery = "show project info" return opt if @help } raise "project is not specified." unless project start project = @agent.projects(project) puts "%s" % project["project"]["id"] puts "--> %s" % project["project"]["git"] puts "--> mysql://%s:%s@%s/%s" % [project["project"]["dbuser"], project["project"]["dbpassword"], "db.phper.jp", project["project"]["dbname"]] @agent.servers(project["project"]["id"]).each { |server| puts "%s\thttp://%s" % [server["server"]["name"],server["server"]["fqdn"]] } end |
#keys ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/phper/commands.rb', line 162 def keys OptionParser.new { |opt| opt.parse!(@command_options) @summery = "list keys" @banner = "" return opt if @help } start @agent.keys.each { |key| name = name_of_key(key["key"]["public_key"]) puts name if name } end |
#keys_add ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/phper/commands.rb', line 177 def keys_add OptionParser.new { |opt| opt.parse!(@command_options) @summery = "add keys" @banner = "<public key file>" return opt if @help } file = @command_options.shift raise "file is not specified." unless file key = "" File.open(file) { |f| key = f.read } name = name_of_key(key) raise "invalid key" unless name start @agent.keys_create(key) puts "key of %s added" % name end |
#keys_clear ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/phper/commands.rb', line 211 def keys_clear OptionParser.new { |opt| opt.parse!(@command_options) @summery = "remove all keys" @banner = "" return opt if @help } start count = @agent.keys_delete_all puts "%i key(s) removed" % [count] end |
#keys_remove ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/phper/commands.rb', line 197 def keys_remove OptionParser.new { |opt| opt.parse!(@command_options) @summery = "remove key" @banner = "<user@host>" return opt if @help } name = @command_options.shift raise "key name is not specified." unless name start count = @agent.keys_delete(name) puts "%i key(s) named %s removed" % [count,name] end |
#list ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/phper/commands.rb', line 72 def list opt = OptionParser.new opt.parse!(@command_options) @summery = "get project list" @banner = "" return opt if @help start @agent.projects.each { |pj| puts pj["project"]["id"] } end |
#login ⇒ Object
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 |
# File 'lib/phper/commands.rb', line 33 def login opt = OptionParser.new opt.parse!(@command_options) @summery = "Login to phper.jp." @banner = "" return opt if @help user = ask('Enter user: ') do |q| q.validate = /\w+/ end ok = false while(!ok) password = ask("Enter your password: ") do |q| q.validate = /\w+/ q.echo = false end @agent.login(user,password) if @agent.login? # => OK ok = true Keystorage.set("phper.jp",user.to_s,password.to_s) puts "login OK" else puts "password mismatch" end end end |
#logout ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/phper/commands.rb', line 62 def logout opt = OptionParser.new opt.parse!(@command_options) @summery = "Logout from phper.jp ." @banner = "" return opt if @help Keystorage.delete("phper.jp") end |
#open ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/phper/commands.rb', line 294 def open project = nil OptionParser.new { |opt| project = extract_project(opt) @banner = "[<host or name pattern>]" @summery = "Open URL" return opt if @help } raise "project is not specified." unless project start servers = @agent.servers(project) raise "project #{project} has no servers." if servers.length == 0 name = @command_options.shift server = nil if name server = servers.find { |s| s["server"]["name"] =~ /^#{name}/ or s["server"]["fqdn"] =~ /^#{name}/ } end server = servers.shift unless server url = "http://#{server["server"]["fqdn"]}" puts "Opening #{url}" Launchy.open url end |
#servers ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/phper/commands.rb', line 223 def servers project = nil OptionParser.new { |opt| project = extract_project(opt) @summery = "list servers" return opt if @help } raise "project is not specified." unless project start @agent.servers(project).each { |server| puts "%s\thttp://%s" % [server["server"]["name"],server["server"]["fqdn"]] } end |
#servers_add ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/phper/commands.rb', line 237 def servers_add param = {} project = nil name = nil OptionParser.new { |opt| opt.on('--name=NAME', 'Name of this server') { |v| param[:name] = v } opt.on('--root=ROOT', 'Document Root') { |v| param[:root] = v } project = extract_project(opt) @banner = "[<host>]" @summery = "add server" return opt if @help } raise "project is not specified." unless project host = @command_options.shift if host param[:fqdn] = host param[:name] = host unless param.has_key?(:name) end start server = @agent.servers_create(project,param)["server"] puts "Created %s" % server["name"] puts "--> http://%s" % server["fqdn"] end |
#servers_clear ⇒ Object
284 285 286 287 288 289 290 291 292 |
# File 'lib/phper/commands.rb', line 284 def servers_clear OptionParser.new { |opt| project = extract_project(opt) @summery = "remove all servers" return opt if @help } raise "project is not specified." unless project end |
#servers_remove ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/phper/commands.rb', line 266 def servers_remove param = {} project = nil name = nil OptionParser.new { |opt| project = extract_project(opt) @banner = "[<host or name>]" @summery = "remove server" return opt if @help } raise "project is not specified." unless project name = @command_options.shift start count = @agent.servers_delete(project,name) puts "%i server(s) named %s removed" % [count,name] end |