source: branches/locker-dev/locker/sql/bin/get-password @ 2080

Last change on this file since 2080 was 1414, checked in by ezyang, 15 years ago
Merged in changes from trunk --- Merging r1262 through r1413 into 'deploy/bin': U deploy/bin/django C deploy/bin/rails > Resolved by accepting working copy --- Merging r1221 through r1413 into '.': U sql/bin/get-password U sql/bin/save-password U doc/tickets/rt.txt U doc/tickets/cnames.txt U bin/fix-php-ini C bin/scripts-rails > Resolved by accepting working copy Skipped 'bin/for-each-server' U bin U sbin/parallel-find.pl U sbin/commit-email.pl U sbin/commit-zephyr Summary of conflicts: Tree conflicts: 2 Skipped paths: 1
  • Property svn:executable set to *
File size: 1.5 KB
Line 
1#!/usr/bin/php
2<?php
3
4$host = 'sql.mit.edu';
5$env_user = getenv('USER');
6$home = '/mit/'.$env_user;
7
8$cnfPath = $home.'/.sql/my.cnf';
9
10function getMyCnfInfo($path) {
11if (file_exists($path)) {
12        global $env_user;
13        $cnfFile = file_get_contents($path);
14        if (preg_match('/\[mysql\][^\[]*host *= *([^\r\n]*)/',$cnfFile,$match)) {
15                $host = $match[1];
16        } elseif (preg_match('/\[client\][^\[]*host *= *([^\r\n]*)/',$cnfFile,$match)) {
17                $host = $match[1];
18        } else {
19                $host = 'sql.mit.edu';
20        }
21        if (preg_match('/\[mysql\][^\[]*user *= *([^\r\n]*)/',$cnfFile,$match)) {
22                $user = $match[1];
23        } elseif (preg_match('/\[client\][^\[]*user *= *([^\r\n]*)/',$cnfFile,$match)) {
24                $user = $match[1];
25        } else {
26                $user = $env_user;
27        }
28        if (preg_match('/\[mysql\][^\[]*password *= *([^\r\n]*)/',$cnfFile,$match)) {
29                $password = $match[1];
30        } elseif (preg_match('/\[client\][^\[]*password *= *([^\r\n]*)/',$cnfFile,$match)) {
31                $password = $match[1];
32        } else {
33                $password = 'password';
34        }
35        return array($host,$user,$password);
36}
37}
38
39$cnfinfo = getMyCnfInfo($cnfPath);
40if (is_array($cnfinfo)) {
41        list($h,$u,$p) = $cnfinfo;
42        echo "$h\t$u\t$p";
43        exit;
44}
45
46$sql_status = file_get_contents('https://sql.mit.edu/main/do/batch/status?u=' . urlencode($env_user));
47switch($sql_status) {
48        case 1:
49                $myPassword = `/usr/bin/sql-signup`;
50        file_put_contents($cnfPath, "[client]\nhost=$host\nuser=$env_user\npassword=$myPassword\n");
51        $cnfinfo = getMyCnfInfo($cnfPath);
52        if (is_array($cnfinfo)) {
53            list($h,$u,$p) = $cnfinfo;
54            echo "$h\t$u\t$p";
55        }
56                break;
57        case 0:
58}
Note: See TracBrowser for help on using the repository browser.