1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 "git.arvados.org/arvados.git/lib/cmd"
22 "git.arvados.org/arvados.git/sdk/go/ctxlog"
26 var Command cmd.Handler = installCommand{}
28 const devtestDatabasePassword = "insecure_arvados_test"
30 type installCommand struct{}
32 func (installCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
33 logger := ctxlog.New(stderr, "text", "info")
34 ctx := ctxlog.Context(context.Background(), logger)
35 ctx, cancel := context.WithCancel(ctx)
41 logger.WithError(err).Info("exiting")
45 flags := flag.NewFlagSet(prog, flag.ContinueOnError)
46 flags.SetOutput(stderr)
47 versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
48 clusterType := flags.String("type", "production", "cluster `type`: development, test, or production")
49 err = flags.Parse(args)
50 if err == flag.ErrHelp {
53 } else if err != nil {
55 } else if *versionFlag {
56 return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
59 var dev, test, prod bool
68 err = fmt.Errorf("cluster type must be 'development', 'test', or 'production'")
72 osv, err := identifyOS()
77 listdir, err := os.Open("/var/lib/apt/lists")
79 logger.Warnf("error while checking whether to run apt-get update: %s", err)
80 } else if names, _ := listdir.Readdirnames(1); len(names) == 0 {
81 // Special case for a base docker image where the
82 // package cache has been deleted and all "apt-get
83 // install" commands will fail unless we fetch repos.
84 cmd := exec.CommandContext(ctx, "apt-get", "update")
102 "daemontools", // lib/boot uses setuidgid to drop privileges when running as root
103 "default-jdk-headless",
104 "default-jre-headless",
113 "libcrypt-ssleay-perl",
114 "libcrypt-ssleay-perl",
116 "libcurl4-openssl-dev",
138 "postgresql-contrib",
151 case osv.Debian && osv.Major >= 10:
152 debs = append(debs, "libcurl4")
154 debs = append(debs, "libcurl3")
156 cmd := exec.CommandContext(ctx, "apt-get", "install", "--yes", "--no-install-recommends")
157 cmd.Args = append(cmd.Args, debs...)
158 cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive")
167 os.Mkdir("/var/lib/arvados", 0755)
168 rubyversion := "2.5.7"
169 if haverubyversion, err := exec.Command("/var/lib/arvados/bin/ruby", "-v").CombinedOutput(); err == nil && bytes.HasPrefix(haverubyversion, []byte("ruby "+rubyversion)) {
170 logger.Print("ruby " + rubyversion + " already installed")
173 mkdir -p /var/lib/arvados/tmp
174 tmp=/var/lib/arvados/tmp/ruby-`+rubyversion+`
175 trap "rm -r ${tmp}" ERR
176 wget --progress=dot:giga -O- https://cache.ruby-lang.org/pub/ruby/2.5/ruby-`+rubyversion+`.tar.gz | tar -C /var/lib/arvados/tmp -xzf -
178 ./configure --disable-install-doc --prefix /var/lib/arvados
181 /var/lib/arvados/bin/gem install bundler
191 if havegoversion, err := exec.Command("/usr/local/bin/go", "version").CombinedOutput(); err == nil && bytes.HasPrefix(havegoversion, []byte("go version go"+goversion+" ")) {
192 logger.Print("go " + goversion + " already installed")
196 wget --progress=dot:giga -O- https://storage.googleapis.com/golang/go`+goversion+`.linux-amd64.tar.gz | tar -C /var/lib/arvados -xzf -
197 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
204 pjsversion := "1.9.8"
205 if havepjsversion, err := exec.Command("/usr/local/bin/phantomjs", "--version").CombinedOutput(); err == nil && string(havepjsversion) == "1.9.8\n" {
206 logger.Print("phantomjs " + pjsversion + " already installed")
209 PJS=phantomjs-`+pjsversion+`-linux-x86_64
210 wget --progress=dot:giga -O- https://bitbucket.org/ariya/phantomjs/downloads/$PJS.tar.bz2 | tar -C /var/lib/arvados -xjf -
211 ln -sf /var/lib/arvados/$PJS/bin/phantomjs /usr/local/bin/
218 geckoversion := "0.24.0"
219 if havegeckoversion, err := exec.Command("/usr/local/bin/geckodriver", "--version").CombinedOutput(); err == nil && strings.Contains(string(havegeckoversion), " "+geckoversion+" ") {
220 logger.Print("geckodriver " + geckoversion + " already installed")
224 wget --progress=dot:giga -O- https://github.com/mozilla/geckodriver/releases/download/$GD/geckodriver-$GD-linux64.tar.gz | tar -C /var/lib/arvados/bin -xzf - geckodriver
225 ln -sf /var/lib/arvados/bin/geckodriver /usr/local/bin/
232 nodejsversion := "v8.15.1"
233 if havenodejsversion, err := exec.Command("/usr/local/bin/node", "--version").CombinedOutput(); err == nil && string(havenodejsversion) == nodejsversion+"\n" {
234 logger.Print("nodejs " + nodejsversion + " already installed")
237 NJS=`+nodejsversion+`
238 wget --progress=dot:giga -O- https://nodejs.org/dist/${NJS}/node-${NJS}-linux-x64.tar.xz | sudo tar -C /var/lib/arvados -xJf -
239 ln -sf /var/lib/arvados/node-${NJS}-linux-x64/bin/{node,npm} /usr/local/bin/
246 gradleversion := "5.3.1"
247 if havegradleversion, err := exec.Command("/usr/local/bin/gradle", "--version").CombinedOutput(); err == nil && strings.Contains(string(havegradleversion), "Gradle "+gradleversion+"\n") {
248 logger.Print("gradle " + gradleversion + " already installed")
252 mkdir -p /var/lib/arvados/tmp
253 zip=/var/lib/arvados/tmp/gradle-${G}-bin.zip
255 wget --progress=dot:giga -O${zip} https://services.gradle.org/distributions/gradle-${G}-bin.zip
256 unzip -o -d /var/lib/arvados ${zip}
257 ln -sf /var/lib/arvados/gradle-${G}/bin/gradle /usr/local/bin/
265 // The entry in /etc/locale.gen is "en_US.UTF-8"; once
266 // it's installed, locale -a reports it as
268 wantlocale := "en_US.UTF-8"
269 if havelocales, err := exec.Command("locale", "-a").CombinedOutput(); err == nil && bytes.Contains(havelocales, []byte(strings.Replace(wantlocale+"\n", "UTF-", "utf", 1))) {
270 logger.Print("locale " + wantlocale + " already installed")
272 err = runBash(`sed -i 's/^# *\(`+wantlocale+`\)/\1/' /etc/locale.gen && locale-gen`, stdout, stderr)
287 if pg_lsclusters, err2 := exec.Command("pg_lsclusters", "--no-header").CombinedOutput(); err2 != nil {
288 err = fmt.Errorf("pg_lsclusters: %s", err2)
290 } else if pgclusters := strings.Split(strings.TrimSpace(string(pg_lsclusters)), "\n"); len(pgclusters) != 1 {
291 logger.Warnf("pg_lsclusters returned %d postgresql clusters -- skipping postgresql initdb/startup, hope that's ok", len(pgclusters))
292 } else if _, err = fmt.Sscanf(pgclusters[0], "%s %s %d %s %s %s %s", &pgc.Version, &pgc.Cluster, &pgc.Port, &pgc.Status, &pgc.Owner, &pgc.DataDirectory, &pgc.LogFile); err != nil {
293 err = fmt.Errorf("error parsing pg_lsclusters output: %s", err)
295 } else if pgc.Status == "online" {
296 logger.Infof("postgresql cluster %s-%s is online", pgc.Version, pgc.Cluster)
298 logger.Infof("postgresql cluster %s-%s is %s; trying to start", pgc.Version, pgc.Cluster, pgc.Status)
299 cmd := exec.Command("pg_ctlcluster", "--foreground", pgc.Version, pgc.Cluster, "start")
307 cmd.Process.Signal(syscall.SIGTERM)
308 logger.Infof("sent SIGTERM; waiting for postgres to shut down")
311 for deadline := time.Now().Add(10 * time.Second); ; {
312 output, err2 := exec.Command("pg_isready").CombinedOutput()
315 } else if time.Now().After(deadline) {
316 err = fmt.Errorf("timed out waiting for pg_isready (%q)", output)
319 time.Sleep(time.Second)
324 if os.Getpid() == 1 {
325 // We are the init process (presumably in a
326 // docker container) so although postgresql is
327 // installed, it's not running, and initdb
328 // might never have been run.
331 withstuff := "WITH LOGIN SUPERUSER ENCRYPTED PASSWORD " + pq.QuoteLiteral(devtestDatabasePassword)
332 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "ALTER ROLE arvados "+withstuff)
334 if err := cmd.Run(); err == nil {
335 logger.Print("arvados role exists; superuser privileges added, password updated")
337 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "CREATE ROLE arvados "+withstuff)
351 type osversion struct {
357 func identifyOS() (osversion, error) {
359 f, err := os.Open("/etc/os-release")
365 kv := map[string]string{}
366 scanner := bufio.NewScanner(f)
368 line := strings.TrimSpace(scanner.Text())
369 if strings.HasPrefix(line, "#") {
372 toks := strings.SplitN(line, "=", 2)
374 return osv, fmt.Errorf("invalid line in /etc/os-release: %q", line)
377 v := strings.Trim(toks[1], `"`)
379 v = strings.Trim(v, `'`)
383 if err = scanner.Err(); err != nil {
392 return osv, fmt.Errorf("unsupported ID in /etc/os-release: %q", kv["ID"])
394 vstr := kv["VERSION_ID"]
395 if i := strings.Index(vstr, "."); i > 0 {
398 osv.Major, err = strconv.Atoi(vstr)
400 return osv, fmt.Errorf("incomprehensible VERSION_ID in /etc/os/release: %q", kv["VERSION_ID"])
405 func runBash(script string, stdout, stderr io.Writer) error {
406 cmd := exec.Command("bash", "-")
407 cmd.Stdin = bytes.NewBufferString("set -ex -o pipefail\n" + script)