<commit_before>module Anjou
  class UserHome
    ANJOU_LOGIN = 'ubuntu'
    SSH_READY_TIMEOUT = 30

    def initialize(username, hostname)
      @username = username
      @hostname = hostname
    end

    def create_linux_user
      ssh_do "sudo adduser --disabled-password --gecos '#{@username}' #{@username}"
      # ssh_do "sudo adduser #{@username} sudo"
      ssh_do "echo '#{@username} ALL=(ALL) NOPASSWD:ALL' > /tmp/sudoize-#{@username}"
      ssh_do "sudo chown root:root /tmp/sudoize-#{@username}"
      ssh_do "sudo chmod 440 /tmp/sudoize-#{@username}"
      ssh_do "sudo mv /tmp/sudoize-#{@username} /etc/sudoers.d/"
    end

    def mount_home_dir
      volume = api.user_volume_for @username
      device = volume.attachments.to_a.first.device.gsub(/sda/, "xvda")
      ssh_do "sudo mount #{device} /home/#{@username}"
    end

    private

    def api
      @api ||= Anjou::EC2.new
    end

    def ssh_do(cmd)
      ensure_ssh_ready
      `#{ssh_cmd} "#{cmd}"`
    end

    def ensure_ssh_ready
      timeout = 0
      until ! `#{ssh_cmd} ls 2>&1`.match(/Connection refused/) do
        raise "ssh connection refused! (retried #{timeout} times)" if timeout >= SSH_READY_TIMEOUT
        puts " - connection refused, waiting a second..."
        sleep 1
        timeout += 1
      end
    end

    def ssh_cmd
      @ssh_cmd ||= "ssh -o 'StrictHostKeyChecking no' #{ANJOU_LOGIN}@#{@hostname}"
    end

  end
end<commit_msg>wait until cloud init is complete<commit_after>module Anjou
  class UserHome
    ANJOU_LOGIN = 'ubuntu'
    SSH_READY_TIMEOUT = 300
    SSH_READY_SLEEP_INCREMENT = 2

    def initialize(username, hostname)
      @username = username
      @hostname = hostname
    end

    def create_linux_user
      ssh_do "sudo adduser --disabled-password --gecos '#{@username}' #{@username}"
      ssh_do "echo '#{@username} ALL=(ALL) NOPASSWD:ALL' > /tmp/sudoize-#{@username}"
      ssh_do "sudo chown root:root /tmp/sudoize-#{@username}"
      ssh_do "sudo chmod 440 /tmp/sudoize-#{@username}"
      ssh_do "sudo mv /tmp/sudoize-#{@username} /etc/sudoers.d/"
    end

    def mount_home_dir
      volume = api.user_volume_for @username
      device = volume.attachments.to_a.first.device.gsub(/sda/, "xvda")
      ssh_do "sudo mount #{device} /home/#{@username}"
    end

    private

    def api
      @api ||= Anjou::EC2.new
    end

    def ssh_do(cmd)
      ensure_ssh_ready
      `#{ssh_cmd} "#{cmd}"`
    end

    def ensure_ssh_ready
      timeout = 0
      until ssh_ready? do
        raise "SSH connection not ready after #{timeout} attempts." if timeout >= (SSH_READY_TIMEOUT / SSH_READY_SLEEP_INCREMENT)
        puts " ...connection not ready, waiting a bit..."
        sleep SSH_READY_SLEEP_INCREMENT
        timeout += 1
      end
    end

    def ssh_ready?
      @ssh_ready ||= `#{ssh_cmd} ls /var/lib/cloud/instance/* 2>&1`.include? 'boot-finished'
    end

    def ssh_cmd
      @ssh_cmd ||= "ssh -o 'StrictHostKeyChecking no' #{ANJOU_LOGIN}@#{@hostname}"
    end

  end
end
{'fraction_non_alphanumeric': 0.12595293337752733, 'fraction_numerical': 0.007623467020218761, 'mean_word_length': 2.649334945586457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1741700', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1051, 'n_words': 252}