Highlighted
Absent Member.
Absent Member.
1173 views

Pytwist functions to export packages

Is there anyway via the ptwist API to export packages to your local filesystem?  Similar to how cbt export works, but via pytwist?

0 Likes
8 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

Not via pytwist.   Here are some alternative way to pull a package from the Word.

To manually fetch something from the word

 /opt/opsware/pylibs/coglib/wordclient --word /packages/opsware/Linux/5SERVER/miniagent /tmp/miniagent

As an agent:

/opt/opsware/agent/pylibs/coglib/wordclient --word /packages/opsware/Linux/5SERVER/miniagent  /dev/null

WGET command line to the CLEAR port (you must be on the core do to this)

wget -O miniagent http://localhost:1006/wordbot.py?file=/packages/opsware/Linux/5SERVER/miniagent

Fetching via the CGW (Core gateway)

curl --insecure --cert /var/opt/opsware/crypto/agent/agent.srv --proxy "localhost:3001" \
https://theword:1003/wordbot.py?file=/packages/opsware/Linux/5SERVER/miniagent > miniagent

 

0 Likes
Highlighted
Absent Member.
Absent Member.

Unfortunately, wordclient can only download files by name when they're in the /packages/opsware folder.  Anything that's stored in the software_repository is stored by an ID, and that ID doesn't match up with the object ID of the package.  The only way I've been able to find that ID is via the OGFS.  Is there a way via pytwist to get that ID?

0 Likes
Highlighted
Honored Contributor.. Honored Contributor..
Honored Contributor..

Here is a function I use to download a file by name.

See the use of spinWrapper. 

Make sure you import it with:

from coglib import spinwrapper

 

 


def download_files(): global downloadedFiles global requestedCount global downloadedCount fileNames="VMware-tools-windows-9.4.15-2827462.zip,vmware_certificate.zip" targetDir="C:\\WINDOWS\\TEMP\\ITR_VMWARE_TOOLS" print "Starting Download Software" if sys.platform == "win32": opswpython = os.path.join(os.environ["ProgramFiles"], "opsware", "agent", "lcpython15", "python.exe") wordclient = os.path.join(os.environ["ProgramFiles"], "opsware", "agent", "pylibs", "coglib", "wordclient.pyc") unzipclient = os.path.join(os.environ["ProgramFiles"], "opsware", "agent", "bin", "unzip.exe") else: wordclient = os.path.join(os.sep, "opt", "opsware", "agent", "pylibs", "coglib", "wordclient") unzipclient = os.path.join(os.sep, "opt", "opsware", "agent", "bin", "unzip") s = spinwrapper.SpinWrapper() wordRequest = r"https://theword:1003/wordbot.py?file=ignored&unit_id=" downloadedCount = 0 fnameList = filter(None, re.split(", *", fileNames)) requestedCount = len(fnameList) for fname in fnameList: if sys.platform == "win32": fname = ntpath.basename(fname) sys.stdout.write("Searching for %s ... " % fname) sys.stdout.flush() hits = s.Unit.getByWildcard(acct_id=25, text="%s" % fname) print hits if len(hits) == 1: unitId = hits[0][0] sys.stdout.write("DONE (unit_id = %s).\nDownloading %s ... " % (unitId, fname)) sys.stdout.flush() cmdargs = ' "' + wordRequest + str(unitId) + '" "' + os.path.join(targetDir, fname) + '"' if sys.platform == 'win32': cmd = '"' + opswpython + '" "' + wordclient + '"' + cmdargs r = os.popen('"' + cmd + '"') else: cmd = wordclient + cmdargs r = os.popen(cmd) rc = r.close() if (rc): warn("Download Error: Trouble transferring file.\n\n") else: print "DONE.\n" downloadedFiles = downloadedFiles + "\"" + os.path.join(targetDir, fname) + "\", " downloadedCount = downloadedCount + 1 elif len(hits) == 0: warn("Download Error: Could not find requested file in repository.\n\n") elif len(hits) > 1: hh=sorted(hits) print hits unitId = hits[0][0] sys.stdout.write("DONE (unit_id = %s).\nDownloading %s ... " % (unitId, fname)) sys.stdout.flush() cmdargs = ' "' + wordRequest + str(unitId) + '" "' + os.path.join(targetDir, fname) + '"' if sys.platform == 'win32': cmd = '"' + opswpython + '" "' + wordclient + '"' + cmdargs r = os.popen('"' + cmd + '"') else: cmd = wordclient + cmdargs r = os.popen(cmd) rc = r.close() if (rc): warn("Download Error: Trouble transferring file.\n\n") else: print "DONE.\n" downloadedFiles = downloadedFiles + "\"" + os.path.join(targetDir, fname) + "\", " downloadedCount = downloadedCount + 1 warn("Download Error: Requested file name must be unique.\n\n") else: warn("Download Error: Unexpected number of files returned from query.\n\n") print "Downloaded Files = %s" % re.sub(", $", "", downloadedFiles)

 

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Sure here is a one-liner to get the ID for the name of something stored in the Word.

[root@sa1020 ~]# PYTHONPATH=/opt/opsware/pylibs /opt/opsware/bin/python -c "from coglib import spinwrapper; print spinwrapper.SpinWrapper().Unit.getAll(restrict={'unit_loc':'/packages/opsware/Linux/5SERVER/miniagent'})[0]['id']"
8740001
[root@sa1020 ~]#

Perhaps if I understood your use case we might find a better solution for what you are trying to accomplish.
Point questions and point answers don't address your requirement.

If you know the ID and the Name of the file you can just make a CURL request directly to get it.

# curl -s -o miniagent -k --cert /var/opt/opsware/crypto/wordbot/wordbot.srv "https://theword:1003/wordbot.py?file=arg&unit_id=8740001"
#

0 Likes
Highlighted
Absent Member.
Absent Member.

We're migrating content out of SA and I want to give my users the ability to quickly export their content, such as a Software Policy, into an easy to grok format that they can import into other systems. The idea was to provide them to something where they could say: .

./export.py -s '/Library/Some Random Path/My Policy'

And it would download the scripts, and the packages within that policy to their local system.

The users do *not* have access to the cores, so this would have to be run from something with an agent.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Can I suggest you use CBT (/opt/opsware/cbt) after all this tool is shipped with the product and its designed to do what you are asking - that is export content from core to another.

I know to use CBT you need core access, perhaps you can wrap it up in an APX or something to allow its execution without users getting cmd line access.  Even with the extra code wrapper it still has to be better than starting from scratch.

 

0 Likes
Highlighted
New Member.

May I know the command to search for the exact package for "getByWildcard" line you have below? I don't want multiple matching files as I know exactly what file name to download.

hits = s.Unit.getByWildcard(acct_id=25, text="%s" % fname)

 

I also tried your method of ".getAll(restrict..)", but it doesn't work for me as I get permission denied message However I am able to search with "getByWildcard" option and get results. 

line: 939
method: checkObjectPerms
module: spinmethods.py
params: {'msg': 'DENIED'}
request: UNKNOWN ....

Is there some documentation on what methods are available for spinwrapper please? I checked in platform develeper guide, twister online documentation etc. but couldn't find anywhere. 

Thank you in advance.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

The Spin API is not customer facing which is why you can't find any documentation on it.
An Agent may not make a Unit.getAll() call as this is a restricted spin API method.
This call may only be made if you have authenticated using the Spin private key which is not available on an Agent.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.