Puppet Class: rustup::global

Defined in:
manifests/global.pp

Summary

Manage global Rust installation with `rustup`

Overview

Examples:

Standard usage

include rustup::global

Parameters:

  • ensure (Enum[present, latest, absent]) (defaults to: present)
    • ‘present` - install rustup, but don’t update it.

    • ‘latest` - install rustup and update it on every puppet run.

    • ‘absent` - uninstall rustup and the tools it manages.

  • user (String[1]) (defaults to: 'rustup')

    The user to own and manage rustup. We recommend not using root or any other existing user.

  • manage_user (Boolean) (defaults to: true)

    Whether or not to manage ‘$user` user.

  • default_toolchain (Optional[String[1]]) (defaults to: undef)

    Which toolchain should be the default.

  • toolchains (Array[String[1]]) (defaults to: [])

    The toolchains to install.

  • purge_toolchains (Boolean) (defaults to: false)

    Whether or not to uninstall toolchains that aren’t managed by Puppet.

  • targets (Array[String[1]]) (defaults to: [])

    The targets to install. These can take two forms:

    * `"$target $toolchain"`: Install `$target` for `$toolchain`.
    * `"$target"`: Install `$target` for the default toolchain.
    

    You can use ‘’default’‘ to indicate the target for the current host.

  • purge_targets (Boolean) (defaults to: false)

    Whether or not to uninstall targets that aren’t managed by Puppet.

  • dist_server (Optional[Stdlib::HTTPUrl]) (defaults to: undef)

    Override ‘RUSTUP_DIST_SERVER`. Set to `’dev-static.rust-lang.org’‘ to install pre-release toolchains.

  • home (Stdlib::Absolutepath) (defaults to: '/opt/rust')

    Where to install rustup and the rust toolchains. Will contain rustup and cargo directories.

  • shell (Stdlib::Absolutepath) (defaults to: '/bin/bash')

    Shell for the rustup user. This can be a nologin shell.

  • env_scripts_append (Array[Stdlib::Absolutepath]) (defaults to: ['/etc/bashrc'])

    Scripts to append with line that sources the cargo environment script.

  • env_scripts_create (Array[Stdlib::Absolutepath]) (defaults to: $facts['os']['family'] ? { 'Darwin' => [], default => ['/etc/profile.d/99-cargo.sh'])

    Paths that will get links to the cargo environment script.

  • installer_source (Stdlib::HTTPUrl) (defaults to: 'https://sh.rustup.rs')

    URL of the rustup installation script. Changing this will have no effect after the initial installation.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'manifests/global.pp', line 45

class rustup::global (
  Enum[present, latest, absent] $ensure             = present,
  String[1]                     $user               = 'rustup',
  Boolean                       $manage_user        = true,
  Optional[String[1]]           $default_toolchain  = undef,
  Array[String[1]]              $toolchains         = [],
  Boolean                       $purge_toolchains   = false,
  Array[String[1]]              $targets            = [],
  Boolean                       $purge_targets      = false,
  Optional[Stdlib::HTTPUrl]     $dist_server        = undef,
  Stdlib::Absolutepath          $home               = '/opt/rust',
  Stdlib::Absolutepath          $shell              = '/bin/bash',
  Array[Stdlib::Absolutepath]   $env_scripts_append = ['/etc/bashrc'],
  Array[Stdlib::Absolutepath]   $env_scripts_create = $facts['os']['family'] ? {
    'Darwin' => [],
    default  => ['/etc/profile.d/99-cargo.sh'],
  },
  Stdlib::HTTPUrl               $installer_source   = 'https://sh.rustup.rs',
) {
  $rustup_home = "${home}/rustup"
  $cargo_home = "${home}/cargo"
  $bin = "${cargo_home}/bin"

  $ensure_simple = $ensure ? {
    'absent'  => absent,
    default => present,
  }

  if $manage_user {
    group { $user:
      ensure => $ensure_simple,
      system => true,
    }

    user { $user:
      ensure     => $ensure_simple,
      comment    => 'rustup',
      gid        => $user,
      home       => $home,
      managehome => false, # FIXME only on macos? need to create our own .bashrc?
      shell      => $shell,
      system     => true,
    }

    if $ensure == absent {
      # Have to delete the user before its primary group.
      User[$user] -> Group[$user]
    }
  }

  if $ensure == absent {
    # rustup { 'rustup': ensure => absent } handles $rustup_home and $cargo_home
    file { $home:
      ensure => absent,
      force  => true,
    }
  } else {
    file { [$home, $rustup_home, $cargo_home]:
      ensure => directory,
      owner  => $user,
      group  => $user,
      mode   => '0755',
      force  => true,
    }
  }

  # Shell init scripts source this to set the environment correctly.
  file { "${home}/env.sh":
    ensure  => $ensure_simple,
    owner   => $user,
    group   => $user,
    mode    => '0555',
    content => epp('rustup/env.sh.epp', {
        bin         => $bin,
        rustup_home => $rustup_home,
        cargo_home  => $cargo_home,
    }),
  }

  # Modify shell init scripts that don’t follow the profile.d pattern.
  $escaped_env_path = stdlib::shell_escape("${home}/env.sh")
  $comment = 'cargo env: managed by Puppet'
  $env_scripts_append.each |$path| {
    file_line { "${path} +source ${home}/env.sh":
      ensure            => $ensure_simple,
      path              => $path,
      line              => ". ${escaped_env_path} # ${comment}",
      match             => "^[.] .* # ${comment}\$",
      match_for_absence => true,
    }
  }

  # Add scripts to /etc/profile.d and similar.
  $link_ensure = $ensure_simple ? {
    'present' => link,
    'absent'  => absent,
  }

  $env_scripts_create.each |$path| {
    file { $path:
      ensure => $link_ensure,
      owner  => 'root',
      group  => '0',
      mode   => '0444',
      target => "${home}/env.sh",
    }
  }

  rustup { $user:
    ensure            => $ensure,
    user              => $user,
    default_toolchain => $default_toolchain,
    toolchains        => $toolchains,
    purge_toolchains  => $purge_toolchains,
    targets           => $targets,
    purge_targets     => $purge_targets,
    dist_server       => $dist_server,
    home              => $home,
    rustup_home       => $rustup_home,
    cargo_home        => $cargo_home,
    modify_path       => false,
    installer_source  => $installer_source,
  }
}