Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run TI Process After SFTP Transfer Complete #119

Closed
ToddGaluska opened this issue Mar 27, 2019 · 3 comments
Closed

Run TI Process After SFTP Transfer Complete #119

ToddGaluska opened this issue Mar 27, 2019 · 3 comments
Labels

Comments

@ToddGaluska
Copy link

Marius,

Sorry to post this here if it should be somewhere else. Have you ever initiated a TI Process after an SFTP transfer completed. I am running a python script on Server #1 to transfer a csv file to Server #2 and after it completes I want a TI process to run. Do you have anything like this I couldnt find anything even though I search the tm1py area.

Thanks

@MariusWirtz
Copy link
Collaborator

Hi @eCapitalAdvisors ,

I have never used this library myself, but for sftp you could perhaps look into pysftp.

Regarding the TI process execution afterwards. This would be fairly simple:

# copy files through sftp

process_name = "Bedrock.Server.Wait"

with TM1Service(**config['tm1srv01']) as tm1:
    success, state, error_log_file = tm1.processes.execute_with_return(
        process_name=process_name, 
        pWaitSec="1")

@ToddGaluska
Copy link
Author

ToddGaluska commented Mar 31, 2019

Marius,
I dont know if this will be helpful to other. I created a python script to transfer a csv from one server to another via SFTP and then run a TI process immediately after. We have always been a little constrained by the TM1/PA Chore scheduler that operates off time. This would create no time delay when the file is dropped and the process is run. Let me know what you think? Also I am loosing the indentation when I paste my code in is there anyway to fix this. Thanks!

import paramiko
import configparser
config = configparser.ConfigParser()
config.read(r'C:\Users\tgaluska\config.ini')
from TM1py import TM1Service

class SSHConnection(object):
    def __init__(self, host, username, password, port=22):
        self.sftp = None
        self.sftp_open = False
        self.transport = paramiko.Transport((host, port))
        self.transport.connect(username="test.user", password="test1234")
    def _openSFTPConnection(self):
        if not self.sftp_open:
            self.sftp = paramiko.SFTPClient.from_transport(self.transport)
            self.sftp_open = True
    def get(self, remote_path, local_path=None):
        self._openSFTPConnection()
        self.sftp.get(remote_path, local_path)
    def put(self, local_path, remote_path=None):
        self._openSFTPConnection()
        self.sftp.put(local_path, remote_path)
    def close(self):
        if self.sftp_open:
            self.sftp.close()
            self.sftp_open = False
        self.transport.close()

if __name__ == "__main__":	
    host = "adaptive1.ecap.local"
    username = "test.user"
    pw = "test1234"
    origin = 'E:\Adaptive\Channels.csv'
    dst = 'C:\Tm1_Models\Blank\sourcefiles\Channels.csv'
    ssh = SSHConnection(host, username, pw)
    ssh.get(origin, dst)
    with TM1Service(**config['Blank']) as tm1_target:
        tm1_target.processes.execute('CreateChannels')
        ssh.close()

@MariusWirtz
Copy link
Collaborator

Hi,

thanks for the code. This is a common use case I think.
Tried to fix the intentation, but not sure if it's better or worse now 😄

generally it's good if your wrap your code like this:

print('Hello World')

Strange... I have copied code samples from my IDE (pycharm) before and the indentation was not removed.

Actually the best way to share it though would be to fork the tm1py-samples repository and create a Merge Request that includes your changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants