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

Fix/libvirt #125

Merged
merged 12 commits into from
Jan 8, 2025
Merged

Fix/libvirt #125

merged 12 commits into from
Jan 8, 2025

Conversation

NichArchA82
Copy link
Collaborator

@NichArchA82 NichArchA82 commented Jan 8, 2025

PR Type

Enhancement, Bug fix


Description

  • Added region and instanceType parameters to libvirt server operations.

  • Enhanced server creation flow with region and instance type selection.

  • Fixed issues with undefined values for region and image.

  • Improved error handling and timeout configurations in libvirt operations.


Changes walkthrough 📝

Relevant files
Enhancement
vm.js
Updated libvirt button actions and parameters                       

command-handler/src/commands/vm.js

  • Added region and instanceType parameters to libvirt operations.
  • Updated button actions to include region and instance type handling.
  • Introduced new button actions for selecting libvirt servers and
    images.
  • Replaced selectImage with selectRegion for libvirt VM creation.
  • +17/-9   
    libvirt-server.js
    Enhanced libvirt server operations and selection methods 

    command-handler/src/util/libvirt/libvirt-server.js

  • Added region and instanceType parameters to server operations.
  • Enhanced server creation with region and instance type selection.
  • Improved error handling for region and server data retrieval.
  • Added new methods for selecting regions, servers, and images.
  • +79/-27 

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling

    The server creation response validation was removed (check for serverRes.data === 'Success'). This could lead to false success scenarios if the API returns an error response.

    let attempts;
    
    let maxRetries = 13;  
    for (attempts = 1; attempts <= maxRetries; attempts++) {
    Potential Bug

    The response?.data optional chaining was added but the error case still tries to use 'data' directly which could cause a runtime error if response is undefined.

    const data = response?.data;
    
    if (!data) {
        app.client.chat.postEphemeral({
          channel: `${body.channel.id}`,

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add validation of server creation response to ensure the operation was successful before proceeding

    Add error handling for the case when the server creation response is not successful.
    Currently, the code continues execution even if the server creation fails.

    command-handler/src/util/libvirt/libvirt-server.js [53-74]

     let serverRes;
     try {
         serverRes = await axios.post(`${process.env.PROVISIONER_URL}/v1/create`, 
             {
                 "vm_name": serverName,
                 "tags": {
                     "owner": userEmail,
                 },
                 "user_data": Buffer.from(configUserData(serverName)).toString('base64'),
                 "image": imageName,
                 "region_name": region,
                 "instance_type": instanceType
             }, {
             headers: {
                 'Authorization': `${process.env.PROVISIONER_API_TOKEN}`,
                 'Content-Type': 'application/json'
             },
             timeout: 1000 * 60 * 5
         });
    +    if (serverRes.data !== 'Success') {
    +        throw new Error('Server creation failed');
    +    }
     } catch (error) {
         log.error('There was an error creating the server', axiosError(error));
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a critical issue where server creation failures might not be properly handled, which could lead to silent failures and inconsistent system state.

    8
    Validate required parameters before attempting server deletion operations

    Add input validation for required parameters (serverName, region) in the
    deleteServer function to prevent potential undefined errors.

    command-handler/src/util/libvirt/libvirt-server.js [126-129]

     deleteServer: async ({ app, body, serverName, region }) => {
    +    if (!serverName || !region) {
    +        throw new Error('Server name and region are required for deletion');
    +    }
         //get servers from tailscale
         const { deviceId } = await getDevices(serverName)
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding parameter validation for server deletion is important for preventing runtime errors and ensuring data integrity during critical deletion operations.

    7
    Enhance region data validation to handle empty region lists

    Add error handling for the case when regions data is undefined or empty in the
    selectRegion function.

    command-handler/src/util/libvirt/libvirt-server.js [290-301]

     const regions = response?.data;
     
    -//return if it fails to get a response from libvirt.
    -if (!regions) {
    +//return if it fails to get a response from libvirt or if regions are empty
    +if (!regions || regions.length === 0) {
     app.client.chat.postEphemeral({
         channel: `${body.channel.id}`,
         user: `${body.user.id}`,
    -    text: `Failed to get region data from libvirt`
    +    text: `Failed to get region data from libvirt or no regions available`
     });
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: The suggestion improves error handling by explicitly checking for empty region lists, preventing potential issues in the region selection flow.

    6

    @NichArchA82 NichArchA82 merged commit 28f8a31 into main Jan 8, 2025
    3 of 4 checks passed
    @NichArchA82 NichArchA82 deleted the fix/libvirt branch January 8, 2025 23:22
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants