module Meroku module CLI HELP = <<~HEREDOC Usage: meroku command subcommand Examples meroku signup # if you havent done already meroku create meroku keys:add meroku infrastrucuture spawn # Spawns server HEREDOC def self.start(*args) case args.join(" ") when "infrastructure spawn" load_secrets || exit node = Meroku::Infrastructure::Node.new.associate_address.install_packages.install_frontend_app puts "spawned #{node.instance.try(:instance_id)}" when "infrastructure despawn" load_secrets || exit Meroku::Infrastructure.despawn when "signup" signup when "keys:add" token_check || exit keys_add when "create" token_check || exit create else puts HELP end end def self.signup print "Email: " email = STDIN.gets print "Password: " password = STDIN.noecho(&:gets).chomp print "\n" url = "https://www.meroku.com/users.json" #url = "http://localhost:3000/users.json" response_json = RestClient.post url, {:user=>{:email => email, :password => password, :password_confirmation => password}}.to_json, timeout: 1, :content_type => :json, :accept => :json if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0 puts JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".") else email = JSON.parse(response_json)["data"]["attributes"]["email"] token = JSON.parse(response_json)["data"]["attributes"]["token"] puts "Signed up #{email}" save_token(token) end end def self.create url = "https://www.meroku.com/apps.json" token = `cat ~/.meroku/.token`.chomp response_json = RestClient.post url, {:app=>{:name => "unnamed"}, :token=>token}.to_json, timeout: 1, :content_type => :json, :accept => :json if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0 puts JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".") else name = JSON.parse(response_json)["data"]["attributes"]["name"] puts "Created #{name}, adding git remote" puts "git remote remove meroku" `git remote remove meroku` puts "git remote add meroku git@www.meroku.com:#{name}.git" `git remote add meroku git@www.meroku.com:#{name}.git` end end def self.token_check if !File.exist? "#{Dir.home}/.meroku/.token" puts "error: Have you logged in yet?" return nil end true end def self.load_secrets if !File.exist? "#{Dir.home}/.meroku/.secret" puts "error: File #{Dir.home}/.meroku/.secret not found" return nil end env_file = Dir.home+'/.meroku/meroku.env' if !File.exist? env_file secret=`cat ~/.meroku/.secret`.chomp remote_env_file = "http://www.sam-we.com/dropbox/meroku-#{secret}/meroku.env" File.write(env_file, Net::HTTP.get(URI.parse(remote_env_file))) end Dotenv.load(env_file) end def self.save_token(token) dirname = File.dirname("#{Dir.home}/.meroku") unless File.directory?(dirname) FileUtils.mkdir(dirname) end File.open("#{Dir.home}/.meroku/.token", 'w') { |file| file.write(token) } end def self.keys_add if !File.exist? "#{Dir.home}/.ssh/id_rsa.pub" puts "error: File #{Dir.home}/.ssh/id_rsa.pub not found" puts "You can use this command to generate a key:" puts " ssh-keygen -t rsa" return nil end name = "id_rsa.pub" data = `cat ~/.ssh/id_rsa.pub`.chomp url = "https://www.meroku.com/publickeys.json" token = `cat ~/.meroku/.token`.chomp response_json = RestClient.post url, {:publickey=>{:name => name, :data=>data}, :token=>token}.to_json, timeout: 1, :content_type => :json, :accept => :json if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0 puts JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".") else name = JSON.parse(response_json)["data"]["attributes"]["name"] puts "Added #{name}" end end end end