whoami.job

Demonstrates using the Exec class to send a command to the operating system’s default command interpreter. On Microsoft Windows, this is cmd.exe. On POSIX systems, this is /bin/sh.

Usage

> zos login --user zenuser
Please enter current password for 'zenuser':
 Logged into grid as zenuser

~> zos jobinfo --detail whoami
Jobname/Parameters    Attributes
------------------    ----------
whoami             Desc: This is a demo example of enhanced exec

    numJoblets     Desc: The number of joblets to schedule
                   Type: Integer
                Default: 1

    resource       Desc: The resource id to run on
                   Type: String
                Default: .*

Description

The files that make up the Whoami job include:

whoami                                           # Total: 118 lines
|-- whoami.jdl                                   #   69 lines
`-- whoami.policy                                #   49 lines

whoami.jdl

 1  # -----------------------------------------------------------------------------
 2  #  Copyright (c) 2008 Novell, Inc. All Rights Reserved.
 3  #
 4  #  NOVELL PROVIDES THE SOFTWARE "AS IS," WITHOUT ANY EXPRESS OR IMPLIED
 5  #  WARRANTY, INCLUDING WITHOUT THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 6  #  FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGMENT.  NOVELL, THE AUTHORS
 7  #  OF THE SOFTWARE, AND THE OWNERS OF COPYRIGHT IN THE SOFTWARE ARE NOT LIABLE
 8  #  FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 9  #  TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
10  #  OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11  # -----------------------------------------------------------------------------
12  #  $Id: whoami.jdl,v 1.3 2008/02/27 20:51:34 john Exp $
13  # -----------------------------------------------------------------------------
14
15  """
16
17  Demonstrate running setuid exec.
18
19  """
20  import os,time
21
22  #
23  # Add to the 'examples' group on deployment
24  #
25  if __mode__ == "deploy":
26      try:
27          jobgroupname = "examples"
28          jobgroup = getMatrix().getGroup(TYPE_JOB, jobgroupname)
29          if jobgroup == None:
30              jobgroup = getMatrix().createGroup(TYPE_JOB, jobgroupname)
31          jobgroup.addMember(__jobname__)
32      except:
33          exc_type, exc_value, exc_traceback = sys.exc_info()
34          print "Error adding %s to %s group: %s %s" % (__jobname__, jobgroupname, exc_type, exc_value)
35
36
37  class Whoami(Job):
38
39       def job_started_event(self):
40            # Launch the joblets
41            numJoblets = self.getFact("jobargs.numJoblets")
42            user = self.getFact("user.id")
43            print "Launching %d joblets for user '%s'" % (numJoblets,user)
44            self.schedule(WhoamiJoblet,numJoblets)
45
46
47  class WhoamiJoblet(Joblet):
48
49       def joblet_started_event(self):
50            if self.getFact("resource.os.family") == "windows":
51                 cmd = "echo %USERNAME%"
52            elif self.getFact("resource.os.family") == "solaris":
53                 cmd = "echo $USER"
54            else:
55                 cmd = "whoami"
56            print "cmd=%s" % (cmd)
57
58            # example using built-in system()
59            #result = system(cmd)
60
61            # example using Exec class
62            e = Exec()
63            e.setShellCommand(cmd)
64            e.writeStdoutToLog()
65            e.writeStderrToLog()
66            result = e.execute()
67
68            print "result=%d" % (result)
69

whoami.policy

 1  <!--
 2   *=============================================================================
 3   * Copyright (c) 2008 Novell, Inc. All Rights Reserved.
 4   *
 5   * NOVELL PROVIDES THE SOFTWARE "AS IS," WITHOUT ANY EXPRESS OR IMPLIED
 6   * WARRANTY, INCLUDING WITHOUT THE IMPLIED WARRANTIES OF MERCHANTABILITY ,
 7   * FITNESS FOR A PARTICULAR PURPOSE, AND NON INFRINGMENT.  NOVELL, THE AUTHORS
 8   * OF THE SOFTWARE, AND THE OWNERS OF COPYRIGHT IN THE SOFTWARE ARE NOT LIABLE
 9   * FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10   * TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
11   * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12   *=============================================================================
13   * $Id: whoami.policy,v 1.2 2008/02/27 20:51:34 john Exp $
14   *=============================================================================
15   -->
16
17  <policy>
18
19       <jobargs>
20
21            <fact name="numJoblets"
22                  type="Integer"
23                  description="The number of joblets to schedule"
24                  value="1" />
25
26            <fact name="resource"
27                  type="String"
28                  description="The resource id to run on"
29                  value=".*" />
30
31       </jobargs>
32
33       <job>
34            <fact name="description"
35                  type="String"
36                  value="This is a demo example of enhanced exec" />
37
38            <!-- only allow one run resource at a time so that multiple re sources can be visited -->
39            <fact name="joblet.maxperresource"
40                  type="Integer"
41                  value="1" />
42       </job>
43
44       <constraint type="resource" >
45          <eq fact="resource.id" factvalue="jobargs.resource" match="regex p" />
46       </constraint>
47
48  </policy>
49

Classes and Methods

Definitions:

Job

A representation of a running job instance.

Joblet

Defines execution on the resource.

MatrixInfo

A representation of the matrix grid object, which provides operations for retrieving and creating grid objects in the system. MatrixInfo is retrieved using the built-in getMatrix() function. Write capability is dependent on the context in which getMatrix() is called. For example, in a joblet process on a resource, creating new grid objects is not supported.

GroupInfo

A representation of Group grid objects. Operations include retrieving the group member lists and adding/removing from the group member lists, and retrieving and setting facts on the group.

Exec

Used to manage command line execution on resources.

Job Details

The following sections describe the Whoami job:

zosadmin deploy

When jobs are deployed into the grid, they can optionally be placed in groups for organization and easy reference. In this case, the Whoami job is added to the group named “examples” (see lines 22-34 of whoami.jdl) and is displayed in the ZENworks Orchestrator Console in the Explorer view at the location:

/ZOS/YOUR_GRID/Jobs/examples.

For a general overview of how jobs are added to groups during deployment, see Walkthrough: Deploy a Sample Job in the Novell ZENworks Orchestrator 1.3 Installation and Getting Started Guide.

job_started_event

When the Whoami job receives a job_started_event, it schedules one or more instances of the Whoami joblet to be run (see line 30 in whoami.jdl). The number of WhoamiJoblet instances is indicated by the value of the numJoblets fact, whose value might have been supplied on the command-line, or referenced from what’s been supplied in the whoami.policy file by default (see lines 3-6 in whoami.policy).

In addition to supplying a default value for numJoblets, the whoami.policy file also supplies a default value for the ID of the resource on which the joblet runs. The default value is .*, which means all resources are included (see lines 7-10 in whoami.policy).

Note that the setting for resource isn’t used in the JDL code but is used to affect which resources on which the joblet run. This occurs because a constraint is specified in whoami.policy that restricts the resources that can run this joblet to the current value of the resource fact (see line 22 in whoami.jdl).

maxperresource is another job setting that affects scheduling of the Whoami joblet. The system uses maxperresource to determine how many instances of the joblet can run simultaneously on the same resource. In this case, only one instance of the Whoami job can be run on a machine at a time, as specified in lines 17-19 in whoami.policy.

When facts are referenced in the JDL file, they are prepended with jobargs. or job.. However, when supplied on the command line, this prefix is omitted. JDL files must use an explicit naming convention when it references facts from the different sections of the policy files. For more information on naming conventions for policy facts, see Section 3.1.1, Naming Orchestrator Job Files.

joblet_started_event

When the Whoami joblet is executed on a particular resource it receives a joblet_started_event. After this happens, the Whoami joblet decides which command to use to get the current username by checking the value of resource.os.family (see lines 36-42 in whoami.jdl). This setting is not set in the whoami.policy, but instead is available from the ZENworks Orchestrator system.

After the command to get the current username has been decided, the ZENworks Orchestrator API class named Exec is used to execute the command on the resource where the joblet is running (see lines 48-54 in whoami.jdl).

By passing the command to the Exec setShellCommand method, the command will be executed by the operating system’s default command interpreter. On Microsoft Windows this cmd.exe. On POSIX systems, this is /bin/sh. As indicated by lines 50-51 in whoami.jdl, all standard out and standard errors are written to the job’s log file.

To view the log file for the whoami job after it has been run, execute the command > zos log whoami.

For more information about using the zos command line, see The zosadmin Command Line Tool. For more information on running commands using the Exec class, see Exec.

Configure and Run

Execute the following commands to deploy and run whoami.job:

  1. Deploy notepad.job into the grid:

    > zosadmin deploy whoami.job
    
  2. Display the list of deployed jobs:

    > zos joblist
    

    whoami should appear in this list.

  3. Run the job on one or more resources using the default values for numJoblets and resource, specified in the whoami.policy file:

    > zos run whoami
    
  4. Run the job on one or more resources using supplied values for numJoblets and resource:

    > zos run whoami numJoblets=10 resource=my_resource_.*
    

    Run 10 joblets simultaneously, but only on resources beginning with the name "my_resource_".

    NOTE:The value for "resource" is specified using regular expression syntax.

See Also