~ubuntu-branches/ubuntu/wily/phabricator/wily

« back to all changes in this revision

Viewing changes to src/workflow/ArcanistInstallCertificateWorkflow.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-11-01 23:20:06 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: package-import@ubuntu.com-20141101232006-mvlnp0cil67tsboe
Tags: upstream-0~git20141101/arcanist
Import upstream version 0~git20141101, component arcanist

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Installs arcanist certificates.
 
5
 */
 
6
final class ArcanistInstallCertificateWorkflow extends ArcanistWorkflow {
 
7
 
 
8
  public function getWorkflowName() {
 
9
    return 'install-certificate';
 
10
  }
 
11
 
 
12
  public function getCommandSynopses() {
 
13
    return phutil_console_format(<<<EOTEXT
 
14
      **install-certificate** [uri]
 
15
EOTEXT
 
16
      );
 
17
  }
 
18
 
 
19
  public function getCommandHelp() {
 
20
    return phutil_console_format(<<<EOTEXT
 
21
          Supports: http, https
 
22
          Installs Conduit credentials into your ~/.arcrc for the given install
 
23
          of Phabricator. You need to do this before you can use 'arc', as it
 
24
          enables 'arc' to link your command-line activity with your account on
 
25
          the web. Run this command from within a project directory to install
 
26
          that project's certificate, or specify an explicit URI (like
 
27
          "https://phabricator.example.com/").
 
28
EOTEXT
 
29
      );
 
30
  }
 
31
 
 
32
  public function getArguments() {
 
33
    return array(
 
34
      '*' => 'uri',
 
35
    );
 
36
  }
 
37
 
 
38
  public function shouldShellComplete() {
 
39
    return false;
 
40
  }
 
41
 
 
42
  public function requiresConduit() {
 
43
    return false;
 
44
  }
 
45
 
 
46
  public function requiresWorkingCopy() {
 
47
    return false;
 
48
  }
 
49
 
 
50
  public function run() {
 
51
    $uri = $this->determineConduitURI();
 
52
    $this->setConduitURI($uri);
 
53
    $configuration_manager = $this->getConfigurationManager();
 
54
 
 
55
    echo "Installing certificate for '{$uri}'...\n";
 
56
 
 
57
    $config = $configuration_manager->readUserConfigurationFile();
 
58
 
 
59
    echo "Trying to connect to server...\n";
 
60
    $conduit = $this->establishConduit()->getConduit();
 
61
    try {
 
62
      $conduit->callMethodSynchronous('conduit.ping', array());
 
63
    } catch (Exception $ex) {
 
64
      throw new ArcanistUsageException(
 
65
        'Failed to connect to server: '.$ex->getMessage());
 
66
    }
 
67
    echo "Connection OK!\n";
 
68
 
 
69
    $token_uri = new PhutilURI($uri);
 
70
    $token_uri->setPath('/conduit/token/');
 
71
 
 
72
    echo "\n";
 
73
    echo phutil_console_format("**LOGIN TO PHABRICATOR**\n");
 
74
    echo "Open this page in your browser and login to Phabricator if ".
 
75
         "necessary:\n";
 
76
    echo "\n";
 
77
    echo "    {$token_uri}\n";
 
78
    echo "\n";
 
79
    echo 'Then paste the token on that page below.';
 
80
 
 
81
 
 
82
    do {
 
83
      $token = phutil_console_prompt('Paste token from that page:');
 
84
      $token = trim($token);
 
85
      if (strlen($token)) {
 
86
        break;
 
87
      }
 
88
    } while (true);
 
89
 
 
90
    echo "\n";
 
91
    echo "Downloading authentication certificate...\n";
 
92
    $info = $conduit->callMethodSynchronous(
 
93
      'conduit.getcertificate',
 
94
      array(
 
95
        'token' => $token,
 
96
        'host'  => $uri,
 
97
      ));
 
98
 
 
99
    $user = $info['username'];
 
100
    echo "Installing certificate for '{$user}'...\n";
 
101
    $config['hosts'][$uri] = array(
 
102
      'user' => $user,
 
103
      'cert' => $info['certificate'],
 
104
    );
 
105
 
 
106
    echo "Writing ~/.arcrc...\n";
 
107
    $configuration_manager->writeUserConfigurationFile($config);
 
108
 
 
109
    echo phutil_console_format(
 
110
      "<bg:green>** SUCCESS! **</bg> Certificate installed.\n");
 
111
 
 
112
    return 0;
 
113
  }
 
114
 
 
115
  private function determineConduitURI() {
 
116
    $uri = $this->getArgument('uri');
 
117
    if (count($uri) > 1) {
 
118
      throw new ArcanistUsageException('Specify at most one URI.');
 
119
    } else if (count($uri) == 1) {
 
120
      $uri = reset($uri);
 
121
    } else {
 
122
      $conduit_uri = $this->getConduitURI();
 
123
      if (!$conduit_uri) {
 
124
        throw new ArcanistUsageException(
 
125
          'Specify an explicit URI or run this command from within a project '.
 
126
          'which is configured with a .arcconfig.');
 
127
      }
 
128
      $uri = $conduit_uri;
 
129
    }
 
130
 
 
131
    $uri = new PhutilURI($uri);
 
132
    $uri->setPath('/api/');
 
133
 
 
134
    return (string)$uri;
 
135
  }
 
136
 
 
137
}