1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 "git.arvados.org/arvados.git/lib/cmd"
23 "git.arvados.org/arvados.git/sdk/go/ctxlog"
27 var Command cmd.Handler = installCommand{}
29 const devtestDatabasePassword = "insecure_arvados_test"
31 type installCommand struct{}
33 func (installCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
34 logger := ctxlog.New(stderr, "text", "info")
35 ctx := ctxlog.Context(context.Background(), logger)
36 ctx, cancel := context.WithCancel(ctx)
42 logger.WithError(err).Info("exiting")
46 flags := flag.NewFlagSet(prog, flag.ContinueOnError)
47 flags.SetOutput(stderr)
48 versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
49 clusterType := flags.String("type", "production", "cluster `type`: development, test, or production")
50 err = flags.Parse(args)
51 if err == flag.ErrHelp {
54 } else if err != nil {
56 } else if *versionFlag {
57 return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
60 var dev, test, prod bool
69 err = fmt.Errorf("invalid cluster type %q (must be 'development', 'test', or 'production')", *clusterType)
74 err = errors.New("production install is not yet implemented")
78 osv, err := identifyOS()
83 listdir, err := os.Open("/var/lib/apt/lists")
85 logger.Warnf("error while checking whether to run apt-get update: %s", err)
86 } else if names, _ := listdir.Readdirnames(1); len(names) == 0 {
87 // Special case for a base docker image where the
88 // package cache has been deleted and all "apt-get
89 // install" commands will fail unless we fetch repos.
90 cmd := exec.CommandContext(ctx, "apt-get", "update")
108 "daemontools", // lib/boot uses setuidgid to drop privileges when running as root
109 "default-jdk-headless",
110 "default-jre-headless",
119 "libcrypt-ssleay-perl",
120 "libcrypt-ssleay-perl",
122 "libcurl4-openssl-dev",
144 "postgresql-contrib",
157 case osv.Debian && osv.Major >= 10:
158 debs = append(debs, "libcurl4")
160 debs = append(debs, "libcurl3")
162 cmd := exec.CommandContext(ctx, "apt-get", "install", "--yes", "--no-install-recommends")
163 cmd.Args = append(cmd.Args, debs...)
164 cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive")
173 os.Mkdir("/var/lib/arvados", 0755)
174 rubyversion := "2.5.7"
175 if haverubyversion, err := exec.Command("/var/lib/arvados/bin/ruby", "-v").CombinedOutput(); err == nil && bytes.HasPrefix(haverubyversion, []byte("ruby "+rubyversion)) {
176 logger.Print("ruby " + rubyversion + " already installed")
179 mkdir -p /var/lib/arvados/tmp
180 tmp=/var/lib/arvados/tmp/ruby-`+rubyversion+`
181 trap "rm -r ${tmp}" ERR
182 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 -
184 ./configure --disable-install-doc --prefix /var/lib/arvados
187 /var/lib/arvados/bin/gem install bundler
197 if havegoversion, err := exec.Command("/usr/local/bin/go", "version").CombinedOutput(); err == nil && bytes.HasPrefix(havegoversion, []byte("go version go"+goversion+" ")) {
198 logger.Print("go " + goversion + " already installed")
202 wget --progress=dot:giga -O- https://storage.googleapis.com/golang/go`+goversion+`.linux-amd64.tar.gz | tar -C /var/lib/arvados -xzf -
203 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
210 pjsversion := "1.9.8"
211 if havepjsversion, err := exec.Command("/usr/local/bin/phantomjs", "--version").CombinedOutput(); err == nil && string(havepjsversion) == "1.9.8\n" {
212 logger.Print("phantomjs " + pjsversion + " already installed")
215 PJS=phantomjs-`+pjsversion+`-linux-x86_64
216 wget --progress=dot:giga -O- https://bitbucket.org/ariya/phantomjs/downloads/$PJS.tar.bz2 | tar -C /var/lib/arvados -xjf -
217 ln -sf /var/lib/arvados/$PJS/bin/phantomjs /usr/local/bin/
224 geckoversion := "0.24.0"
225 if havegeckoversion, err := exec.Command("/usr/local/bin/geckodriver", "--version").CombinedOutput(); err == nil && strings.Contains(string(havegeckoversion), " "+geckoversion+" ") {
226 logger.Print("geckodriver " + geckoversion + " already installed")
230 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
231 ln -sf /var/lib/arvados/bin/geckodriver /usr/local/bin/
238 nodejsversion := "v8.15.1"
239 if havenodejsversion, err := exec.Command("/usr/local/bin/node", "--version").CombinedOutput(); err == nil && string(havenodejsversion) == nodejsversion+"\n" {
240 logger.Print("nodejs " + nodejsversion + " already installed")
243 NJS=`+nodejsversion+`
244 wget --progress=dot:giga -O- https://nodejs.org/dist/${NJS}/node-${NJS}-linux-x64.tar.xz | sudo tar -C /var/lib/arvados -xJf -
245 ln -sf /var/lib/arvados/node-${NJS}-linux-x64/bin/{node,npm} /usr/local/bin/
252 gradleversion := "5.3.1"
253 if havegradleversion, err := exec.Command("/usr/local/bin/gradle", "--version").CombinedOutput(); err == nil && strings.Contains(string(havegradleversion), "Gradle "+gradleversion+"\n") {
254 logger.Print("gradle " + gradleversion + " already installed")
258 mkdir -p /var/lib/arvados/tmp
259 zip=/var/lib/arvados/tmp/gradle-${G}-bin.zip
261 wget --progress=dot:giga -O${zip} https://services.gradle.org/distributions/gradle-${G}-bin.zip
262 unzip -o -d /var/lib/arvados ${zip}
263 ln -sf /var/lib/arvados/gradle-${G}/bin/gradle /usr/local/bin/
271 // The entry in /etc/locale.gen is "en_US.UTF-8"; once
272 // it's installed, locale -a reports it as
274 wantlocale := "en_US.UTF-8"
275 if havelocales, err := exec.Command("locale", "-a").CombinedOutput(); err == nil && bytes.Contains(havelocales, []byte(strings.Replace(wantlocale+"\n", "UTF-", "utf", 1))) {
276 logger.Print("locale " + wantlocale + " already installed")
278 err = runBash(`sed -i 's/^# *\(`+wantlocale+`\)/\1/' /etc/locale.gen && locale-gen`, stdout, stderr)
293 if pg_lsclusters, err2 := exec.Command("pg_lsclusters", "--no-header").CombinedOutput(); err2 != nil {
294 err = fmt.Errorf("pg_lsclusters: %s", err2)
296 } else if pgclusters := strings.Split(strings.TrimSpace(string(pg_lsclusters)), "\n"); len(pgclusters) != 1 {
297 logger.Warnf("pg_lsclusters returned %d postgresql clusters -- skipping postgresql initdb/startup, hope that's ok", len(pgclusters))
298 } 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 {
299 err = fmt.Errorf("error parsing pg_lsclusters output: %s", err)
301 } else if pgc.Status == "online" {
302 logger.Infof("postgresql cluster %s-%s is online", pgc.Version, pgc.Cluster)
304 logger.Infof("postgresql cluster %s-%s is %s; trying to start", pgc.Version, pgc.Cluster, pgc.Status)
305 cmd := exec.Command("pg_ctlcluster", "--foreground", pgc.Version, pgc.Cluster, "start")
313 cmd.Process.Signal(syscall.SIGTERM)
314 logger.Info("sent SIGTERM; waiting for postgres to shut down")
317 err = waitPostgreSQLReady()
323 if os.Getpid() == 1 {
324 // We are the init process (presumably in a
325 // docker container) so although postgresql is
326 // installed, it's not running, and initdb
327 // might never have been run.
330 var needcoll []string
331 // If the en_US.UTF-8 locale wasn't installed when
332 // postgresql initdb ran, it needs to be added
333 // explicitly before we can use it in our test suite.
334 for _, collname := range []string{"en_US", "en_US.UTF-8"} {
335 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-t", "-c", "SELECT 1 FROM pg_catalog.pg_collation WHERE collname='"+collname+"' AND collcollate IN ('en_US.UTF-8', 'en_US.utf8')")
337 out, err2 := cmd.CombinedOutput()
339 err = fmt.Errorf("error while checking postgresql collations: %s", err2)
342 if strings.Contains(string(out), "1") {
343 logger.Infof("postgresql supports collation %s", collname)
345 needcoll = append(needcoll, collname)
348 if len(needcoll) > 0 && os.Getpid() != 1 {
349 // In order for the CREATE COLLATION statement
350 // below to work, the locale must have existed
351 // when PostgreSQL started up. If we're
352 // running as init, we must have started
353 // PostgreSQL ourselves after installing the
354 // locales. Otherwise, it might need a
355 // restart, so we attempt to restart it with
357 if err = runBash(`sudo systemctl restart postgresql`, stdout, stderr); err != nil {
358 logger.Warn("`systemctl restart postgresql` failed; hoping postgresql does not need to be restarted")
359 } else if err = waitPostgreSQLReady(); err != nil {
363 for _, collname := range needcoll {
364 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "CREATE COLLATION \""+collname+"\" (LOCALE = \"en_US.UTF-8\")")
370 err = fmt.Errorf("error adding postgresql collation %s: %s", collname, err)
375 withstuff := "WITH LOGIN SUPERUSER ENCRYPTED PASSWORD " + pq.QuoteLiteral(devtestDatabasePassword)
376 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "ALTER ROLE arvados "+withstuff)
378 if err := cmd.Run(); err == nil {
379 logger.Print("arvados role exists; superuser privileges added, password updated")
381 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "CREATE ROLE arvados "+withstuff)
395 type osversion struct {
401 func identifyOS() (osversion, error) {
403 f, err := os.Open("/etc/os-release")
409 kv := map[string]string{}
410 scanner := bufio.NewScanner(f)
412 line := strings.TrimSpace(scanner.Text())
413 if strings.HasPrefix(line, "#") {
416 toks := strings.SplitN(line, "=", 2)
418 return osv, fmt.Errorf("invalid line in /etc/os-release: %q", line)
421 v := strings.Trim(toks[1], `"`)
423 v = strings.Trim(v, `'`)
427 if err = scanner.Err(); err != nil {
436 return osv, fmt.Errorf("unsupported ID in /etc/os-release: %q", kv["ID"])
438 vstr := kv["VERSION_ID"]
439 if i := strings.Index(vstr, "."); i > 0 {
442 osv.Major, err = strconv.Atoi(vstr)
444 return osv, fmt.Errorf("incomprehensible VERSION_ID in /etc/os-release: %q", kv["VERSION_ID"])
449 func waitPostgreSQLReady() error {
450 for deadline := time.Now().Add(10 * time.Second); ; {
451 output, err := exec.Command("pg_isready").CombinedOutput()
454 } else if time.Now().After(deadline) {
455 return fmt.Errorf("timed out waiting for pg_isready (%q)", output)
457 time.Sleep(time.Second)
462 func runBash(script string, stdout, stderr io.Writer) error {
463 cmd := exec.Command("bash", "-")
464 cmd.Stdin = bytes.NewBufferString("set -ex -o pipefail\n" + script)