| 89 | require 'rubygems' |
| 90 | require 'mysql2' |
| 91 | |
| 92 | # All these hardcoded values need to be imported somehow. |
| 93 | dbhost = "myboss" |
| 94 | dbuser = "mysql" |
| 95 | dbname = "tbdb" |
| 96 | pid = "Deter" |
| 97 | |
| 98 | Chef::Log.info("Connecting to db #{dbname} on #{dbhost} as #{dbuser}") |
| 99 | client = Mysql2::Client.new(:host => dbhost, :username => dbuser, :database => dbname) |
| 100 | Chef::Log.info("Connected. Querying for appropriate user accounts.") |
| 101 | |
| 102 | # ugly SQL stolen from tmcd.c |
| 103 | # to do this right, we need to analyise all the user cases from tmcd.c for |
| 104 | # account creation and add them here (or the ones we want to support anyway. |
| 105 | stmt = "select distinct " |
| 106 | stmt << " u.uid,u.usr_pswd,u.unix_uid,u.usr_name, " |
| 107 | stmt << " p.trust,g.pid,g.gid,g.unix_gid,u.admin, " |
| 108 | stmt << " u.emulab_pubkey,u.home_pubkey, " |
| 109 | stmt << " UNIX_TIMESTAMP(u.usr_modified), " |
| 110 | stmt << " u.usr_email,u.usr_shell, " |
| 111 | stmt << " u.widearearoot,u.wideareajailroot, " |
| 112 | stmt << " u.usr_w_pswd,u.uid_idx " |
| 113 | stmt << "from group_membership as p " |
| 114 | stmt << "join users as u on p.uid_idx=u.uid_idx " |
| 115 | stmt << "join groups as g on " |
| 116 | stmt << " p.pid=g.pid and p.gid=g.gid " |
| 117 | stmt << "where ((p.pid='#{pid}')) and p.trust!='none' " |
| 118 | stmt << " and u.status='active' " |
| 119 | stmt << " and u.webonly=0 " |
| 120 | stmt << " and g.unix_gid is not NULL " |
| 121 | stmt << "order by u.uid" |
| 122 | |
| 123 | results = client.query(stmt) |
| 124 | |
| 125 | results.each do | row | |
| 126 | user row['uid'] do |
| 127 | Chef::Log.info("Creating account for #{row['uid']} (#{row['usr_name']})") |
| 128 | supports :manage_home => false # do not create home dir, it'll be mounted from ops |
| 129 | supports :non_unique => false # so not allow multiple account with like uids. |
| 130 | password row['usr_pswd'] |
| 131 | shell "/bin/#{row['usr_shell']}" # TODO fix path here what if shell is not in /bin? |
| 132 | home "/users/#{row['uid']}" |
| 133 | uid row['unix_uid'] |
| 134 | gid row['unix_gid'] |
| 135 | username row['uid'] |
| 136 | if row['admin'] |
| 137 | Chef::Log.info("#{row['uid']} is an admin account.") |
| 138 | system true |
| 139 | end |
| 140 | action :create |
| 141 | end |
| 142 | end |
| 143 | |