Sha256: 4ed6a3d7b7409f4979f720b47c2ab0a08fc99056749ccb9486a5839b07e5d1fa

Contents?: true

Size: 1.52 KB

Versions: 17

Compression:

Stored size: 1.52 KB

Contents

require "bard/github"

# generate and install ssh public key into deploy keys
# add repo to known hosts
# clone repo

class Bard::Provision::Repo < Bard::Provision
  def call
    print "Repo:"
    if !already_cloned?
      if !can_clone_project?
        if !ssh_keypair?
          print " Generating keypair in ~/.ssh,"
          provision_server.run! "ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -q -N \"\"", home: true
        end
        print " Add public key to GitHub repo deploy keys,"
        title = "#{server.ssh_uri.user}@#{server.ssh_uri.host}"
        key = provision_server.run "cat ~/.ssh/id_rsa.pub", home: true
        Bard::Github.new(server.project_name).add_deploy_key title:, key:
      end
      print " Cloning repo,"
      provision_server.run! "git clone git@github.com:botandrosedesign/#{project_name}", home: true
    end

    puts " ✓"
  end

  private

  def ssh_keypair?
    provision_server.run "[ -f ~/.ssh/id_rsa.pub ]", home: true, quiet: true
  end

  def already_cloned?
    provision_server.run "[ -d ~/#{project_name}/.git ]", home: true, quiet: true
  end

  def can_clone_project?
    github_url = "git@github.com:botandrosedesign/#{project_name}"
    provision_server.run [
      "needle=$(ssh-keyscan -t ed25519 github.com 2>/dev/null | cut -d \" \" -f 2-3)",
      "grep -q \"$needle\" ~/.ssh/known_hosts || ssh-keyscan -H github.com >> ~/.ssh/known_hosts 2>/dev/null",
      "git ls-remote #{github_url}",
    ].join("; "), home: true, quiet: true
  end

  def project_name
    server.project_name
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
bard-1.3.9 lib/bard/provision/repo.rb
bard-1.3.8 lib/bard/provision/repo.rb
bard-1.3.7 lib/bard/provision/repo.rb
bard-1.3.6 lib/bard/provision/repo.rb
bard-1.3.5 lib/bard/provision/repo.rb
bard-1.3.4 lib/bard/provision/repo.rb
bard-1.3.3 lib/bard/provision/repo.rb
bard-1.3.2 lib/bard/provision/repo.rb
bard-1.3.1 lib/bard/provision/repo.rb
bard-1.3.0 lib/bard/provision/repo.rb
bard-1.2.0 lib/bard/provision/repo.rb
bard-1.1.2 lib/bard/provision/repo.rb
bard-1.1.1 lib/bard/provision/repo.rb
bard-1.1.0 lib/bard/provision/repo.rb
bard-1.0.8 lib/bard/provision/repo.rb
bard-1.0.7 lib/bard/provision/repo.rb
bard-1.0.6 lib/bard/provision/repo.rb