The Sauce Labs Cookbook

Sauce Headless

Front End Performance Testing

Insights

External Resources

More Info


Page tree
Skip to end of metadata
Go to start of metadata

For Linux users, systemd and upstart are service management tools that facilitate Sauce Connect Proxy tunnel monitoring, system startup/shutdown, and rolling restarts. 

When used to perform rolling restarts, systemd and upstart allow time for Sauce Connect Proxy to clean up on exit, making it a more fluid experience. This is useful in scenarios where you want to kill and quickly restart a new Sauce Connect Proxy instance; you can use systemd and upstart to schedule and control the timing of shutdown and clean-up processes. 

See the following sections for more information:

Setting Up systemd

  1. Create a system user to run the Sauce Connect Proxy.

    sudo adduser --system --no-create-home --group --disabled-login --home /nonexistent sauceconnect
  2. Download Sauce Connect Proxy. 

    wget https://saucelabs.com/downloads/sc-<VERSION>-linux.tar.gz
  3. Untar the Sauce Connect Proxy file. 

    tar -zxvf sc-<VERSION>-linux.tar.gz
  4. Copy the Sauce Connect Proxy file into a dedicated directory.

    cp sc-<VERSION>-linux/bin/sc /usr/local/bin/sc
  5. Copy the systemd config files.

    cp sc-<VERSION>-linux/config_examples/systemd/ /etc/systemd/system
  6. In the system directory, edit the service file named sc@.service.
    In the User=nobody and Group=nobody lines, you will need to replace nobody the with the name of the system user you created in Step 1.
    In the Environment lines, you will need to replace username and the placeholder string with your Sauce Labs username and access key. 

    [Unit]
    Description=Sauce Connect worker service on port %i
    PartOf=sc.service
    ReloadPropagatedFrom=sc.service
    
    [Service]
      Type=simple
      User=nobody
      Group=nobody
      WorkingDirectory=/tmp
      LimitNOFILE=8192
      Restart=always
    
      # Set those to match your Saucelabs credentials
      Environment=SAUCE_USERNAME=username
      Environment=SAUCE_ACCESS_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    
      ExecStart = /usr/local/bin/sc \
                    --logfile - \
                    --pidfile "/tmp/sauceconnect_%i.pid" \
                    --se-port "%i" \
                    --no-remove-colliding-tunnels
      # Not needed with systemd
      ExecStartPost = /bin/rm -f /tmp/sauceconnect_%i.pid
    
    [Install]
    WantedBy=multi-user.target
  7. Create a directory sc.service.wants in /etc/systemd/system. You'll have to create symbolic links inside this directory to set up new instances of Sauce Connect Proxy. For example, if you'd like to have two Sauce Connect Proxy instances listening on port 8000 and 8001, your script would look something like this.

    sudo cd /etc/systemd/system/
    sudo mkdir -p ./sc.service.wants
    sudo ln -s /etc/systemd/system/sc@.service ./sc.service.wants/sc@8000.service
    sudo ln -s /etc/systemd/system/sc@.service ./sc.service.wants/sc@8001.service
    
  8. Reload the service file.

    sudo systemctl daemon-reload
  9. Start the service.

    sudo systemctl start sc
  10. Check the status of the service.

    sudo systemctl status sc
  11. You can also check the status of the individual instances.

    sudo systemctl status 'sc@*'
  12. You can stop the service with this command.

    sudo systemctl stop sc



Setting Up upstart

  1. Navigate to your /local/bin directory where you want to install Sauce Connect Proxy.

    cd /usr/local/bin
  2. Download Sauce Connect Proxy. 

    wget https://saucelabs.com/downloads/sc-<VERSION>-linux.tar.gz
  3. Untar the Sauce Connect Proxy file. 

    tar -zxvf sc-<VERSION>-linux.tar.gz
  4. Copy the Sauce Connect Proxy file into a dedicated directory.

    cp sc-<VERSION>-linux/bin/sc
  5. Make sure Sauce Connect Proxy is located in the correct directory.

    ls /usr/local/bin/sc
  6. Change to the /etc/init directory.

    cd /etc/init
  7. In the /etc/init directory, create a file sc.conf with these contents.
    Change the username and access key in the file to match your own.  

    sc.conf example file
    #
    #This Upstart config expects that Sauce Connect is installed at
    #/usr/local/bin/sc. Edit that path if it is installed somewhere else.
    #
    #Copy this file to /etc/init/sc.conf, and do:
    #
    # $ sudo initctl reload-configuration
    #
    #Then you can manage SC via the usual upstart tools, e.g.:
    #
    #$ sudo start sc
    #$ sudo restart sc
    #$ sudo stop sc
    #$ sudo status sc
    #
    start on filesystem and started networking
    stop on runlevel 06
    
    respawn
    respawn limit 15 5
    
    #Wait for tunnel shutdown when stopping Sauce Connect.
    kill timeout 120
    
    #Bump maximum number of open files/sockets.
    limit nofile 8192 8192
    
    #Make Sauce Connect output go to /var/log/upstart/sc.log.
    console log
    
    env LOGFILE="/tmp/sc_long.log"
    env PIDFILE="/tmp/sc_long.pid"
    env SAUCE_USERNAME="CHANGEME" # XXX
    env SAUCE_ACCESS_KEY="CHANGEME" # XXX
    
    post-start script
      # Save the pidfile, since Sauce Connect might remove it before the
      # post-stop script gets a chance to run.
      n=0
      max_tries=30
      while [ $n -le $max_tries ]; do
        if [ -f $PIDFILE ]; then
          cp $PIDFILE ${PIDFILE}.saved
          break
        fi
        n=$((n+1))
        [ $n -ge $max_tries ] && exit 1
        sleep 1
      done
    end script
    
    post-stop script
      # Wait for Sauce Connect to shut down its tunnel.
      n=0
      max_tries=30
      pid="$(cat ${PIDFILE}.saved)"
      while [ $n -le $max_tries ]; do
        kill -0 $pid || break
        n=$((n+1))
        [ $n -ge $max_tries ] && exit 1
        sleep 1
      done
    end script
    
    setuid nobody
    setgid nogroup
    
    chdir /tmp
    
    exec /usr/local/bin/sc -l $LOGFILE --pidfile $PIDFILE
  8. Reload the service.

    sudo initctl reload-configuration
  9. Start the service.

    sudo start sc
  10. Check the status of the service.

    sudo status sc
  11. You can stop the service with this command.

    sudo stop sc