Class: Chulai::Base
- Inherits:
-
Object
- Object
- Chulai::Base
- Defined in:
- lib/chulai/base.rb
Constant Summary collapse
- API_BASE_URL =
"http://wo.chulai.la"
- GIT_ROOT =
"git.chulai.la"
- SSH_KEY =
"chulai"
- SUFFIX =
"chulai.la"
Instance Attribute Summary collapse
-
#identity ⇒ Object
readonly
Returns the value of attribute identity.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #account ⇒ Object
- #account_check ⇒ Object
- #account_sign_in ⇒ Object
- #auth_token ⇒ Object
- #deploy ⇒ Object
- #gemfile ⇒ Object
- #git ⇒ Object
-
#initialize(root = '.') ⇒ Base
constructor
A new instance of Base.
- #open ⇒ Object
- #push ⇒ Object
- #ssh_config ⇒ Object
- #ssh_key ⇒ Object
Constructor Details
Instance Attribute Details
#identity ⇒ Object (readonly)
Returns the value of attribute identity.
5 6 7 |
# File 'lib/chulai/base.rb', line 5 def identity @identity end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
5 6 7 |
# File 'lib/chulai/base.rb', line 5 def token @token end |
Instance Method Details
#account ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/chulai/base.rb', line 19 def account if Git.global_config.keys.include? 'chulai.token' account_check else account_sign_in end end |
#account_check ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/chulai/base.rb', line 49 def account_check puts "checking account" @auth_token = Git.global_config 'chulai.token' res = http_without_auth :post, "/account_check.json", auth_token: @auth_token @username = res["username"] if res["status"] == 'success' [@username, @auth_token] else puts "error: #{res["message"]}" exit end end |
#account_sign_in ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/chulai/base.rb', line 62 def account_sign_in puts "please sign in" print "email: " email = $stdin.gets.chomp print "password: " password = STDIN.noecho(&:gets).chomp puts res = http_without_auth :post, "/account_sign_in.json", user: { email: email, password: password } if res["status"] == 'success' @username = res["username"] @auth_token = res["auth_token"] Git.global_config 'chulai.token', @auth_token else puts "error: #{res["message"]}" end end |
#auth_token ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/chulai/base.rb', line 27 def auth_token if @auth_token @auth_token else account end @auth_token end |
#deploy ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/chulai/base.rb', line 162 def deploy puts "deploying" stream :post, "/deploy.stream", identity: identity, commit: git.log.first.sha , comment: git.log.first., gems: @gems do |chunk| puts chunk end rescue => exc puts "deploy failed: #{exc.to_s}" exit end |
#gemfile ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/chulai/base.rb', line 122 def gemfile puts "checking Gemfile" unless File.exist? 'Gemfile' puts "Error: Gemfile not found, please make sure you are in a Rails project dir" exit end @gemfile = Gemnasium::Parser::Gemfile.new File.read("Gemfile") @gems = @gemfile.dependencies.map(&:name) unless @gems.include? 'rails' puts "Error: chulai ONLY support Rails projects by now" exit end end |
#git ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/chulai/base.rb', line 36 def git return @git if @git @git = Git.open @root#, log: Logger.new(STDOUT) unless @git.log.first.to_s == @git.branches[:master].gcommit.log.first.to_s puts "Error: please checkout to master branch first" exit end @git rescue => e puts "Error: git not initailzed" exit end |
#open ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/chulai/base.rb', line 173 def open url = "http://#{@username}.#{@name}.#{SUFFIX}" Launchy.open url rescue => exc puts "failed to launch any browser: #{exc.to_s}" puts url exit end |
#push ⇒ Object
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/chulai/base.rb', line 136 def push puts "pushing to git server" remote = git.remote(:chulai) if remote.url.nil? res = http :post, "/birth.json" raise res.inspect unless res['status'] == 'success' @identity, @name = res["identity"], res["name"] git.add_remote :chulai, "#{SSH_KEY}:#{@identity}.git" git.config 'chulai.identity', @identity git.config 'chulai.name', @name else @identity = git.config 'chulai.identity' res = http :post, "/check.json", identity: @identity raise res.inspect unless res && res['status'] == 'success' @name = res["name"] end git.push(git.remote("chulai")) rescue => exc puts "failed to push code to git server: #{exc.to_s}" exit end |
#ssh_config ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/chulai/base.rb', line 102 def ssh_config puts "checking ssh config" config_dir = File. "~/.ssh" Dir.mkdir config_dir unless Dir.exist? config_dir config_path = File.join config_dir, "config" unless File.exist?(config_path) and Net::SSH::Config.load(config_path, SSH_KEY).length > 1 File.open config_path, "a" do |f| f.write [ "", "Host #{SSH_KEY}", "HostName #{GIT_ROOT}", "User git", "IdentityFile ~/.ssh/#{SSH_KEY}", "", ].join("\n") end end end |
#ssh_key ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/chulai/base.rb', line 83 def ssh_key puts "checking ssh key" key_dir = File. "~/.ssh" Dir.mkdir key_dir unless Dir.exist? key_dir key_path = File.join key_dir, SSH_KEY key = SSHKey.new File.read key_path rescue => e puts "sshkey ~/.ssh/chulai not found, generate a new one" key = SSHKey.generate File.open(key_path, "w") do |f| f.write key.private_key f.chmod 0400 end File.open("#{key_path}.pub", "w") do |f| f.write key.ssh_public_key end res = http :post, "/keys.json", key: {name: Socket.gethostname, content: key.ssh_public_key} end |